in the CLASS definition here, why isn't def __init__ ... there???? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

in the CLASS definition here, why isn't def __init__ ... there????

class Spam: __egg = 7 def print_egg(self): print(self.__egg) s = Spam() s.print_egg() print(s._Spam__egg) print(s.__egg)

18th Aug 2016, 1:02 PM
Krishna Limani
Krishna Limani - avatar
8 Answers
+ 2
is it because that since there's NOT ANY ATTRIBUTES AT ALL aside from the obvious self attribute, we'd just have def __init(self)__, in which case it's totally fine and OK to skip this??? an exceptional case where you can skip doing this def __int__ bit, due to no attributes aside from the trivial self attribute
18th Aug 2016, 1:05 PM
Krishna Limani
Krishna Limani - avatar
+ 1
thanks !
18th Aug 2016, 1:37 PM
Krishna Limani
Krishna Limani - avatar
+ 1
hang on! isn't "s", where s=Spam(), an instance of the class Spam? I'm confused still, didn't quite understand what you were saying there I'm afraid. Plz further explain...
18th Aug 2016, 2:50 PM
Krishna Limani
Krishna Limani - avatar
0
The __init__ method is typically used to initialise an instance of the class. You don't need it if you don't need to initialise anything.
18th Aug 2016, 1:25 PM
Gershon Fosu
Gershon Fosu - avatar
0
I think there is nothing to initialise other than self .. and we don't need to initialise self
11th May 2017, 1:57 AM
sandeep
sandeep - avatar
0
__init__..is used to call the class automatically without any object declaration
12th Apr 2020, 9:04 AM
Mannam Rama krishna
Mannam Rama krishna - avatar
0
you only declared a variable and I do not see any object in your code? 🧐🧐
2nd Oct 2020, 8:34 PM
Shreyaansh
Shreyaansh - avatar
0
so that is why their is no def __init__():
2nd Oct 2020, 8:34 PM
Shreyaansh
Shreyaansh - avatar