using super() to call two parent classes __init__() method

Alex Martelli aleax at mac.com
Fri Aug 17 01:39:21 EDT 2007


7stud <bbxx789_05ss at yahoo.com> wrote:

> When I run the following code and call super() in the Base class's
> __init__ () method,  only one Parent's __init__() method is called.
> 
> 
> class Parent1(object):
>     def __init__(self):
>         print "Parent1 init called."
>         self.x = 10
> 
> class Parent2(object):
>     def __init__(self):
>         print "Parent2 init called."
>         self.y = 15
> 
> class Base(Parent1, Parent2):
>     def __init__(self):
>         super(Base, self).__init__()
>         self.z = 20
> 
> b = Base()
> 
> --output:--
> Parent1 init called.

Yep -- Parent1.__init__ doesn't call its own super's __init__, so it
doesn't participate in cooperative superclass delegation and "the buck
stops there".


Alex



More information about the Python-list mailing list