Re: [Tutor] Using superclass __init__ method
Hi, paul brian wrote on 22.09.2005: > >class Base: > def __init__(self): > print "hello" > >class Child(Base): > def __init__(self): > Base.__init__(self) > This is how I did it so far. But in my program, I have 10 subclasses with identical __init__ methods, so I can simplify the setup a lot. Thanks for your help, Jan -- Alcohol and calculus don't mix - PLEASE don't drink and derive. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using superclass __init__ method
Kent Johnson wrote on 22.09.2005: >This is standard behavior for any class attribute - if it's not >defined in the derived class, the base class is checked. It's not >special for __init__. > >I can't find a comprehensive reference for the way attribute lookup >works - anyone else? Well, I did know that any attribute used explicitly with a class instance is looked up in the class, then the base class etc: class Base: variable = 1 class Child(Base): pass child = Child() print child.variable 1 What I did not know was that the __init__ method behaves the same way - i.e. that Python looks for __init__ upon instantiation until it finds one. Thanks, Jan -- A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using superclass __init__ method
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 instance) from newstyle tutorial (comment mine): class B: def m(self): print "B here" class C(B): def m(self): 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: class Base: def __init__(self): print "hello" class Child(Base): def __init__(self): Base.__init__(self) produces:: >>> c = Child() hello This might also help make things clearer class Base: def __init__(self): print "hello" class Child(Base): def __init__(self): Base.__init__(self) def foo(self): print "foo" c = Child() b = Base() x = Child() x.foo() Child.foo(x) #1 Child.foo(b) #2 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? -- Paul Brian m. 07875 074 534 t. 0208 352 1741 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using superclass __init__ method
Jan Eden wrote: > Hi, > > I just noticed that in the following environment: > > class Base: > def __init__(self): > ... > > class Child(Base): > pass > > the following statement: > > child = Child() > > would automatically execute the superclass __init__ method. This is exactly > what I was looking for - but in the Cookbook, I found several suggestions on > how to actively force the invocation of the superclass __init__. > > Is this behvaiour documented? This is standard behavior for any class attribute - if it's not defined in the derived class, the base class is checked. It's not special for __init__. I can't find a comprehensive reference for the way attribute lookup works - anyone else? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using superclass __init__ method
Hi, I just noticed that in the following environment: class Base: def __init__(self): ... class Child(Base): pass the following statement: child = Child() would automatically execute the superclass __init__ method. This is exactly what I was looking for - but in the Cookbook, I found several suggestions on how to actively force the invocation of the superclass __init__. Is this behvaiour documented? Thanks, Jan -- There are 10 kinds of people: those who understand binary, and those who don't ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor