(python)class without __init__ still works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
14th Dec 2021, 3:14 AM
Lea
Lea - avatar
20 Answers
+ 11
Simon Sauter Lea The problem here is the terminology. Many people call __init__ a constructor. But __init__ is not a constructor! It plays the role of a constructor, but is essentially just an initializer. It allows you to configure a ready-made object. A constructor returns a new object. A __init__ call, on the other hand, will simply change the properties of an existing object.
14th Dec 2021, 4:01 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 5
Lea Yes it will work without _init_ but when you want to create an instance of class then it is required. Since you have static method so no need to create instance of class, you can directly call function through class name
14th Dec 2021, 3:21 AM
A͢J
A͢J - avatar
+ 5
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ actually you can still create instances even if you don't explicitly define __init__(). I think in that case python implicitly uses the __init__() method of the (featureless) object class.
14th Dec 2021, 3:43 AM
Simon Sauter
Simon Sauter - avatar
+ 3
In Python, the __init__() method plays the role of a constructor when creating an instance of a class. In your case, the object is not created, you just call a static method of the class. You might as well just take it out of the class.
14th Dec 2021, 3:20 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 3
__init__ is like a constructor
15th Dec 2021, 8:34 PM
Arevik Khachaturian
Arevik Khachaturian - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Ok, so if i create a Rectangle, I won’t get its area with out __init__(), right?
14th Dec 2021, 3:37 AM
Lea
Lea - avatar
+ 2
Alexey Kopyshev Haha yes, you are right, i’m learning staticmethod, that’s why, thank you for helping me!
14th Dec 2021, 3:42 AM
Lea
Lea - avatar
+ 2
Simon Sauter It's true. Explicitly writing __init__ makes it possible to set default parameters and object behavior, but even without explicit __init__ will be called when the object is created
14th Dec 2021, 3:50 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 2
Just add these lines to your code to see: a = Shape() print(a)
14th Dec 2021, 3:50 AM
Simon Sauter
Simon Sauter - avatar
+ 2
The reason is that in Python 3 all classes inherit from the object class. (In Python 2 there are old-style classes which do not inherit from the object class and new-style classes which do.)
14th Dec 2021, 3:52 AM
Simon Sauter
Simon Sauter - avatar
14th Dec 2021, 3:53 AM
Simon Sauter
Simon Sauter - avatar
+ 2
Alexey Kopyshev Simon Sauter Thank you for all the info shared. Those really improve my understanding about __init__. Thank you for helping me!
14th Dec 2021, 11:24 PM
Lea
Lea - avatar
+ 1
Alexey Kopyshev Excuse me, take what out of the class? The staticmethod?
14th Dec 2021, 3:35 AM
Lea
Lea - avatar
+ 1
Lea I mean, if you only have one static method and nothing else, there's no point in creating a class.
14th Dec 2021, 3:40 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 1
Simon Sauter What!? Really? Could you show me a simple example?
14th Dec 2021, 3:49 AM
Lea
Lea - avatar
+ 1
__init__ is for create a variable of the class
15th Dec 2021, 1:01 AM
CGO!
CGO! - avatar
16th Dec 2021, 2:11 AM
Romeo Cojocaru
Romeo Cojocaru - avatar
- 4
What do you mean by html
16th Dec 2021, 2:32 AM
Lucky
Lucky - avatar
- 4
In python command line how can we save the file
16th Dec 2021, 2:35 AM
Lucky
Lucky - avatar
- 5
yeah that good I like the deatlis and if eneyone sees this check my codeing
15th Dec 2021, 9:21 PM
chill pillows
chill pillows - avatar