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

Steve Holden steve at holdenweb.com
Fri Aug 17 02:05:01 EDT 2007


7stud 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.
> 
If you want super() to work for you then all your classes have to use 
it. There is only one call to an __init__() method in your definitions - 
Parent1 and Parent2 should also be calling their super().__init__().

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list