r/learnpython • • Oct 07 '20

Classes in Python

Hey,

what is the best way to learn using classes in Python? Until now, I was using functions for almost every problem I had to solve, but I suppose it's more convenient to use classes when problems are more complex.

Thanks in advance!

324 Upvotes

97 comments sorted by

View all comments

143

u/IvoryJam Oct 07 '20

I didn't get classes either, until I really learned the power of them. Think about a class as a template to color in, you can then reuse that template over and over to make different objects.

Say you have two enemies and want to have one of them lose health, no the basic way to do this is with dictionaries.

enemy_1 = {
    'health': 100,
    'attack': 100,
}
enemy_2 = {
    'health': 100,
    'attack': 100,
}
print(enemy_1['health'])
enemy_1['health'] -= 10
print(enemy_1['health'])

so now you can compare the two enemies' health, but then what if you want 100 enemies? That's gonna be a lot of code! Instead you can make one class and just make new enemies when you want them. (I even threw in a way for you to take damage)

class enemy_template:
    def __init__(self):
        self.health = 100
        self.attack = 100

    def damage(self, take_damage=0):
        self.health -= take_damage

enemy_1 = enemy_template()
enemy_2 = enemy_template()
print(enemy_1.health)
enemy_1.damage(10)
print(enemy_1.health)

The trick to understanding them more is to start using them more. Find an API and make your own module for it, build a game like I showed you. "How can I do ______ in python classes"?

25

u/nathan_wolfe2208 Oct 08 '20

What’s the purpose of the init function, I was watching a tutorial on classes but was confused by that.

0

u/[deleted] Oct 08 '20

[deleted]

15

u/nathan_wolfe2208 Oct 08 '20

So is it required for the class to work, like I know in almost any case it would make sense to use it but I’m just curious if it is like required.

17

u/callmelucky py34int Oct 08 '20

Not required, but 95% of the time it makes sense to implement, as it allows you to initialise an object and some or all of its attributes in one readable swoop.

7

u/Finally_Adult Oct 08 '20

It is not required.

8

u/Corrin_Zahn Oct 08 '20

Yep, Python just sort of handwaves __init__ until you actually want to do something with it yourself.

3

u/Pythonistar Python 3.7+ Oct 08 '20

Python just sort of handwaves init

Pretty much every OOP language "handwaves" the constructor. It's known as the "default constructor" and it is implied when you create the class.

2

u/WillardWhite Oct 08 '20

Yep, Python just sort of handwaves __init__ until you actually want to do something with it yourself.

AAAccccchhhhhtuuuallly: you inherit the base one from the class object. so not handwaved at all. very clearly defined, just in the parent class

6

u/Luxi36 Oct 08 '20

If you're used to other program languages, it works the same as a constructor. Not mandatory, but when used it's automatically called first, by the class.

2

u/sw85 Oct 08 '20

level 3flyingwizard119 hours agoWhenever you create a new object that is an instance of a class, the init function is run.So for example, in the example they mentioned above, when it says enemy_1=enemy_template(), the init function is run and sets the health to 100 and attack to 100. The purpose of it is usually to do stuff like setting initial values.

It is 'required' in the sense that all classes have 'init' functions which are run whenever the class is instantiated. It is not required in the sense that you don't have to define an 'init' function yourself. If you don't define one, a default 'init' function is defined (or, more precisely, inherited from the parent 'class' object) containing only the 'pass' statement (meaning that nothing happens when the class is instantiated, beyond the instance automatically having access to all the methods in the class; this is useful if, for instance, your class is nothing more than a container for methods).

9

u/KW__REDDIT Oct 08 '20

Actually to be 100% accurate init function is run when object is already created and is run once, at the beginning of the life of an object. Function that is run when object is being created is called new() and so it is run before init(). That is tiny difference and init has gained more reputation and both can be used almost the same way but still I think it is good to know more than less.

→ More replies (2)

13

u/Finally_Adult Oct 08 '20

Additionally you can pass parameters into a class when you create it and the init function is called the constructor and it’s where the parameters go.

So if you pass in health and set self.health = health then when you created enemy_one = enemy(100) its health will be 100 and enemy_two = enemy(200) its health will be 200.

7

u/nathan_wolfe2208 Oct 08 '20

Ah that’s sick, makes a lot more sense

2

u/[deleted] Oct 08 '20

I dont want to split hairs but technically the init function is not a constructor. The object is already constructed when init gets called. It's similar to a constructor but it would be more accurate to call it an initializer.

→ More replies (1)

8

u/gmorf33 Oct 08 '20

Init is the constructor, or the instructions on how to create the object when you instantiate the class. Any variables (properties) or initial logic, data structures etc that you want setup automatically when the object is created.

2

u/nathan_wolfe2208 Oct 08 '20

Thanks, also would it be possible for you to explain what @staticmethod does and what the @ symbol means, and I believe there is another besides staticmethod but I forgot what it was.

→ More replies (4)
→ More replies (2)

3

u/IvoryJam Oct 08 '20

It INITializes it, stuff to do at the beginning that every new object (enemy in our case) gets setup with. In our case we wanted every enemy to have 100 health and 100 attack.

→ More replies (8)

4

u/Fission_Mailed_2 Oct 08 '20

but then what if you want 100 enemies? That's gonna be a lot of code!

Why is it? You could still create 100 enemies easily in one for loop without a lot of code.

enemies = []
for _ in range(100):
    enemy = {"health": 100, "attack": 100}
    enemies.append(enemy)

Now you could access each enemy by index in the enemies list. You could even use the random module to set random values for the health and attack values if you wanted.

Just to be clear, I'm just playing devil's advocate here, I'm not saying I would use a list of dictionaries to represent enemies instead of using a class, I'm just showing that there is an alternative way that doesn't require a lot of code.

→ More replies (3)

2

u/id_H1K4RU Oct 08 '20

Would you mind explaining why you put "=0" in "damage(self, take_damage=0)"? It works without setting damage to 0, right? Thanks! :)

3

u/LogisticAI Oct 08 '20

Putting “take_damage=0” sets the default amount of damage that the enemy object will take if you call the enemy.damage() method without any arguments. You override the default when you call the method with the argument. So “enemy.damage(20)” sets “take_damage=20”

→ More replies (1)

2

u/Anshu_79 Oct 08 '20

You sir, are a very good teacher. Thank you!

1

u/[deleted] Oct 08 '20

Assuming your health got to zero, how would you terminate the enemy's life cause you wouldn't want to keep going in negative values. Do you just del the enemy variable (enemy_1) you created or there's some other way?

I perfectly understand everything you wrote up there.

3

u/fiddle_n Oct 08 '20

It's important to distinguish between "knowing when an enemy is dead", vs "removing an enemy from memory". Also, this is less about enemy instances, and more about the code that uses them.

If an enemy's life is below 0, then you can use this information to declare the enemy dead. Any time the player attempts a fight, they can perform this check, and if the health is below 0, then you know the enemy is dead and you do no further action with it. (In reality, you would probably have code to ensure health never actually drops below 0, and you might have a flag called is_alive that is set to False if health is 0, and then you check this flag rather than checking health directly, but the concept remains the same).

del is used to remove objects from memory. You can use it to remove your enemy object from memory, but rarely is this the correct answer. The correct answer depends upon the situation in which the enemy objects are used.

For example, If you know you only have a set number of enemies to deal with (e.g. exactly 100), you might just create them and not worry about the memory usage. But if you are always creating enemies, then this might not be viable and you might run out of memory after a while depending on the design of the game.

If, however, the game always has battles, but you only create three enemies at a time, you could have a function that represents a single battle, where you create three enemies. Then, when the battle ends, if you return from the function, the enemy objects will be removed from memory by Python's garbage collector as nothing is using them anymore.

→ More replies (1)

1

u/Sigg3net Oct 08 '20

I made the same "mistake" as you on my first large project using a dictionary like this. It should be noted that the thinking is not bad, however, because an object instance's properties is really a dictionary. See:

enemy_1.__dict__

if memory serves.

I wanted to use a dictionary because it's a hash map and therefore fast, but so are objects :)

Python is just awesome.

→ More replies (4)