[Tutor] Using superclass __init__ method
paul1brian at gmail.com
Thu Sep 22 16:28:42 CEST 2005
There are several areas this seems to touch upon, most of them well
covered by Guido himself in http://www.python.org/2.2/descrintro.html
firstly to call a super class' methods you get the subclass to refer
to the *superclass* then the method (note not to the superclass
from newstyle tutorial (comment mine):
print "B here"
print "C here"
B.m(self) #refer to the class definition not the
instance of C via self.
Secondly to run __init__ as you have described is normal behaviour
because the __call__ function that every new instance must go through
makes sure that __init__ is called, and as there is not one defined in
subclass it uses the one in superclass.
This should have the same effect, but is more controllable:
>>> c = Child()
This might also help make things clearer
c = Child()
b = Base()
x = Child()
The class defintion of Child is an object (since 2.2) and can be
called with parameters. at #1 we see foo printed out because we have
called the Child object method foo with an object of type Child (this
is essentially self)
Of course we can try passing in another object (#2) but it will barf.
This issue is frankly just muddy. Not because of bad design or poor
documentation but because it is a awkward subject. Read the link
above - most of us mortals have had to read it several times. Things
also get a little more complex with __new__ but its all in there
However I have not found a really good explanation of the whole
instance creation thing - perhaps this list could make one?
m. 07875 074 534
t. 0208 352 1741
More information about the Tutor