multiple inheritance with builtins

G

Giovanni Bajo

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:

.... def __init__(self):
.... print "A.__init__"
.... super(A, self).__init__()
........ def __init__(self):
.... print "B.__init__"
.... super(B, self).__init__()
....
B.__init__
A.__init__
[]
.... def __init__(self):
.... print "C.__init__"
.... super(C, self).__init__()
....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?
 
S

Steve Holden

Giovanni said:
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:



... def __init__(self):
... print "A.__init__"
... super(A, self).__init__()
...

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

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

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top