multiple inheritance with builtins

Discussion in 'Python' started by Giovanni Bajo, Mar 6, 2005.

  1. Hello,

    I noticed that bultin types like list, set, dict, tuple don't seem to adhere to
    the convention of using super() in constructor to correctly allow
    diamond-shaped inheritance (through MRO). For instance:


    >>> class A(object):

    .... def __init__(self):
    .... print "A.__init__"
    .... super(A, self).__init__()
    ....
    >>> class B(A, list):

    .... def __init__(self):
    .... print "B.__init__"
    .... super(B, self).__init__()
    ....
    >>> B.__mro__

    (<class '__main__.B'>, <class '__main__.A'>, <type 'list'>, <type 'object'>)
    >>> B()

    B.__init__
    A.__init__
    []
    >>> class C(list, A):

    .... def __init__(self):
    .... print "C.__init__"
    .... super(C, self).__init__()
    ....
    >>> C.__mro__

    (<class '__main__.C'>, <type 'list'>, <class '__main__.A'>, <type 'object'>)
    >>> C()

    C.__init__
    []



    It seems weird to me that I have to swap the order of bases to get the expected
    behaviour. Is there a reason for this, or is it simply a bug that should be
    fixed?
    --
    Giovanni Bajo
    Giovanni Bajo, Mar 6, 2005
    #1
    1. Advertising

  2. Giovanni Bajo

    Steve Holden Guest

    Giovanni Bajo wrote:
    > Hello,
    >
    > I noticed that bultin types like list, set, dict, tuple don't seem to adhere to
    > the convention of using super() in constructor to correctly allow
    > diamond-shaped inheritance (through MRO). For instance:
    >
    >
    >
    >>>>class A(object):

    >
    > ... def __init__(self):
    > ... print "A.__init__"
    > ... super(A, self).__init__()
    > ...
    >
    >>>>class B(A, list):

    >
    > ... def __init__(self):
    > ... print "B.__init__"
    > ... super(B, self).__init__()
    > ...
    >
    >>>>B.__mro__

    >
    > (<class '__main__.B'>, <class '__main__.A'>, <type 'list'>, <type 'object'>)
    >
    >>>>B()

    >
    > B.__init__
    > A.__init__
    > []
    >
    >>>>class C(list, A):

    >
    > ... def __init__(self):
    > ... print "C.__init__"
    > ... super(C, self).__init__()
    > ...
    >
    >>>>C.__mro__

    >
    > (<class '__main__.C'>, <type 'list'>, <class '__main__.A'>, <type 'object'>)
    >
    >>>>C()

    >
    > C.__init__
    > []
    >
    >
    >
    > It seems weird to me that I have to swap the order of bases to get the expected
    > behaviour. Is there a reason for this, or is it simply a bug that should be
    > fixed?


    The documentation explicitly states that only one of the built-in types
    can be used as a base class: they aren't desinged to be mixed with each
    other.

    regards
    Steve
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.pycon.org/
    Steve Holden http://www.holdenweb.com/
    Steve Holden, Mar 6, 2005
    #2
    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. Sean 'Shaleh' Perry

    Re: Least used builtins?

    Sean 'Shaleh' Perry, Jul 4, 2003, in forum: Python
    Replies:
    2
    Views:
    315
    Lulu of the Lotus-Eaters
    Jul 5, 2003
  2. Raymond Hettinger
    Replies:
    8
    Views:
    295
    Daniel 'Dang' Griffith
    Apr 21, 2004
  3. Raymond Hettinger
    Replies:
    0
    Views:
    296
    Raymond Hettinger
    Apr 19, 2004
  4. Kamilche
    Replies:
    5
    Views:
    331
    Kamilche
    Jun 10, 2004
  5. Kenneth McDonald
    Replies:
    2
    Views:
    278
    Russell Blau
    Jun 16, 2004
Loading...

Share This Page