+ 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)
8 odpowiedzi
+ 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
+ 1
thanks !
+ 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...
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.
0
I think there is nothing to initialise other than self .. and we don't need to initialise self
0
__init__..is used to call the class automatically without any object declaration
0
you only declared a variable and I do not see any object in your code? 🧐🧐
0
so that is why their is no def __init__():