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

Show parent comments

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.

1

u/alokinsakraf00 Oct 08 '20

Is the init function a constructor then?