Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   multiple inheritance with builtins (http://www.velocityreviews.com/forums/t342291-multiple-inheritance-with-builtins.html)

Giovanni Bajo 03-06-2005 03:03 AM

multiple inheritance with builtins
 
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



Steve Holden 03-06-2005 07:20 PM

Re: multiple inheritance with builtins
 
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/


All times are GMT. The time now is 02:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.