multiple inheritance with builtins
Steve Holden
steve at holdenweb.com
Sun Mar 6 14:20:35 EST 2005
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/
More information about the Python-list
mailing list