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

Discussion in 'Python' started by 7stud, Aug 17, 2007.

  1. 7stud

    7stud Guest

    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.
    7stud, Aug 17, 2007
    #1
    1. Advertising

  2. 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.


    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
    Alex Martelli, Aug 17, 2007
    #2
    1. Advertising

  3. 7stud

    Steve Holden Guest

    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 -------------
    Steve Holden, Aug 17, 2007
    #3
  4. 7stud

    Evan Klitzke Guest

    On 8/16/07, 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.


    As the other posters have mentioned, each class needs to make a call
    to super. This is because the super call doesn't really call the
    parent class' method, it actually calls the next method in the MRO.
    The MRO in this case will be Base -> Parent1 -> Parent2 -> object. You
    can read the details of Python's MRO here
    http://www.python.org/download/releases/2.3/mro/

    --
    Evan Klitzke <>
    Evan Klitzke, Aug 18, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,757
    Darryl Pierce
    Feb 24, 2005
  2. Steven Bethard
    Replies:
    2
    Views:
    454
    Steven Bethard
    Feb 16, 2005
  3. Kent Johnson
    Replies:
    7
    Views:
    908
    Jan Niklas Fingerle
    Feb 12, 2006
  4. zweibieren

    A need for super.super.method()

    zweibieren, May 7, 2009, in forum: Java
    Replies:
    0
    Views:
    448
    zweibieren
    May 7, 2009
  5. Ramchandra Apte
    Replies:
    17
    Views:
    335
    Manuel Pégourié-Gonnard
    Sep 30, 2012
Loading...

Share This Page