multiple inheritance

Discussion in 'Python' started by Thomas Girod, Feb 15, 2006.

  1. Thomas Girod

    Thomas Girod Guest

    Hi.

    I think I'm missing something about multiple inheritance in python.

    I've got this code.

    class Foo:
    def __init__(self):
    self.x = "defined by foo"
    self.foo = None

    class Bar:
    def __init__(self):
    self.x = "defined by bar"
    self.bar = None

    class Foobar(Foo,Bar):
    pass

    fb = Foobar()
    print fb.x
    print fb.__dict__

    which returns :

    >>>

    defined by foo
    {'x': 'defined by foo', 'foo': None}

    So I guess not defining __init__ in my class Foobar will call __init__
    from my superclass Foo. Also __dict__ doesn't show an attribute
    'bar':None so I guess Bar.__init__ is not called at all.

    I would like to have a subclass with all attributes from superclasses
    defined, and only the attribute from the first when there is conflict
    (i.e. in this case, Foobar would be like this but with bar:None)

    I tried this :

    class Foobar(Foo,Bar):
    def __init__(self):
    Foo.__init__(self)
    Bar.__init__(self)

    >>>

    defined by bar
    {'x': 'defined by bar', 'foo': None, 'bar': None}

    Here I have all I want, except the value of 'x' comes from the 'Bar'
    superclass rather than Foo. So, to have what I want, I would have to
    invert the two calls to __init__ in order to have the right x value.

    What I find awkward here is that the order of __init__ calls matters,
    rather than the order of the classes in the class declaration.

    Do you have any ideas of a way to get this multiple inheritance thing
    solved without having to do those __init__ calls ?

    Thomas
     
    Thomas Girod, Feb 15, 2006
    #1
    1. Advertising

  2. Thomas Girod a écrit :

    > Hi.
    >
    > I think I'm missing something about multiple inheritance in python.
    >
    > I've got this code.
    >
    > class Foo:
    > def __init__(self):
    > self.x = "defined by foo"
    > self.foo = None
    >
    > class Bar:
    > def __init__(self):
    > self.x = "defined by bar"
    > self.bar = None
    >
    > class Foobar(Foo,Bar):
    > pass
    >
    > fb = Foobar()
    > print fb.x
    > print fb.__dict__
    >
    > which returns :
    >
    > >>>

    > defined by foo
    > {'x': 'defined by foo', 'foo': None}
    >
    > So I guess not defining __init__ in my class Foobar will call __init__
    > from my superclass Foo. Also __dict__ doesn't show an attribute
    > 'bar':None so I guess Bar.__init__ is not called at all.
    >
    > I would like to have a subclass with all attributes from superclasses
    > defined, and only the attribute from the first when there is conflict
    > (i.e. in this case, Foobar would be like this but with bar:None)
    >
    > I tried this :
    >
    > class Foobar(Foo,Bar):
    > def __init__(self):
    > Foo.__init__(self)
    > Bar.__init__(self)
    >
    > >>>

    > defined by bar
    > {'x': 'defined by bar', 'foo': None, 'bar': None}
    >
    > Here I have all I want, except the value of 'x' comes from the 'Bar'
    > superclass rather than Foo. So, to have what I want, I would have to
    > invert the two calls to __init__ in order to have the right x value.
    >
    > What I find awkward here is that the order of __init__ calls matters,
    > rather than the order of the classes in the class declaration.
    >
    > Do you have any ideas of a way to get this multiple inheritance thing
    > solved without having to do those __init__ calls ?


    try:

    class Foo(object):
    def __init__(self):
    super(Foo, self).__init__() # not really necessary here
    self.x = "defined by foo"

    class Bar(object):
    def __init__(self):
    super(Bar, self).__init__()
    self.x = "defined by bar"

    class FooBar(Foo, Bar):
    def __init__(self):
    super(FooBar, self).__init__()


    and search for the "cooperative methods and super" section
    in http://www.python.org/2.2/descrintro.html

    Cheers,

    SB


    >
    > Thomas
     
    =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=, Feb 15, 2006
    #2
    1. Advertising

  3. Thomas Girod

    Guest

    Hi Thomas,

    When an object is created, the __init__ function will be called. Since
    you didn't define it in Foobar, the search path finds the __init__
    function in Foo, so that's the one that is called. The second __init__
    in Bar is masked since it comes second in the inheritance list..

    If you want to call both constructors, then what you did is fine! The
    fact that 'x' comes from the Bar object is ok too. That's what you told
    python to do. What did you expect if you are setting 'x' twice? I mean,
    when I type the following two statements into the interpreter:

    a = 1
    a = 2

    then after those two statements execute, the value of a is
    naturally 2, not 1. Same with your 'x'.

    And what is wrong with the explicit calls to each constructor? After
    all, in python, explicit is better than implicit. If you don't believe
    me,
    type "import this" in your interpreter:


    >>> import this

    The Zen of Python, by Tim Peters

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
     
    , Feb 15, 2006
    #3
  4. Jan Niklas Fingerle, Feb 16, 2006
    #4
  5. Thomas Girod

    Thomas Girod Guest

    thanks, you pointed exactly on what distrurbed me. I'll see what I can
    do with cooperative methods.
     
    Thomas Girod, Feb 16, 2006
    #5
  6. Thomas Girod

    Thomas Girod Guest

    That's perfect. thanks.
     
    Thomas Girod, Feb 16, 2006
    #6
    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. maxw_cc
    Replies:
    1
    Views:
    3,159
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,053
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,925
    Mike Schilling
    Feb 27, 2008
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    613
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page