[Tutor] Confirmation about __init__()
Lie Ryan
lie.1296 at gmail.com
Mon Jan 18 07:47:36 CET 2010
On 01/17/10 16:42, Robert wrote:
> I have read quite a bit in the past 2 months, ( I have also looked at codes)
> At this point, I think I understand well what __init__() is and does -
> But, I have yet to see this *specifically* spelled out about the the
> __init__ method for a Class;
>
> It is OPTIONAL, correct ?
>
> if I have NO start values/variables to set, no Base Class __init__ to
> call --- the __init__ method is NOT required, correct ?
No, __init__ is always required for new-style classes; however due to
object inheritance (all new-style classes inherits from `object`), all
new-style classes inherits the do-nothing __init__ from `object`.
Essentially, it means __init__ can be omitted if it does the same as the
super class' __init__.
More generally, a method (e.g. MyClass.__init__) that does not override
the superclass' method (e.g. MySuperClass.__init__) will use the
superclass' definition of the method (i.e. when MyClass inherits from
MySuperClass, and __init__ is not overrided in MyClass, MyClass.__init__
== MySuperClass.__init__).
More information about the Tutor
mailing list