The __init__ magic method arguments | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The __init__ magic method arguments

Hello everyone, I'm learning Python and currently trying to embrace the OOP section. Can someone please explain how it affects my created class, if I don't state the arguments I use in the brackets in the headline right after "__init__"? Here's some example. https://code.sololearn.com/c65cXcwCAGgS/?ref=app

14th Dec 2020, 10:41 PM
Dimitry Buylin
Dimitry Buylin - avatar
7 Answers
+ 1
That means you are creating three variables, right when the class is created
15th Dec 2020, 12:06 AM
Gizmo-Lang
Gizmo-Lang - avatar
+ 1
those are not arguments
15th Dec 2020, 12:06 AM
Gizmo-Lang
Gizmo-Lang - avatar
0
__init__ is a constructor for a class, meaning that whenever an instance of that class is created, that function is called right away
14th Dec 2020, 10:45 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
Every __init__ definition must have “self” as an argument or it won’t be associated with the class
14th Dec 2020, 10:50 PM
Gizmo-Lang
Gizmo-Lang - avatar
0
Okay, but what about the line 3 in the example code: def __init__(self): I don't put any arguments in the brackets except for 'self'. Yet I can still use them as usual. What difference it makes when I do state them and when I do not? Except for 'self', I have there '_desc', 'class_name', 'health'.
15th Dec 2020, 12:00 AM
Dimitry Buylin
Dimitry Buylin - avatar
0
So this also means that I set new values for them each time I create new object. Thank you man! I think I understood it now.
15th Dec 2020, 1:05 AM
Dimitry Buylin
Dimitry Buylin - avatar
0
Yup!
15th Dec 2020, 1:31 AM
Gizmo-Lang
Gizmo-Lang - avatar