← index #2028Issue #6077
Related · high · value 0.433
QUERY · ISSUE

super() appears not to work with multiple inheritance

openby peterhinchopened 2016-05-03updated 2016-06-19
bug

This derives from this forum post.

class A:
    def __init__(self):
        print("A.__init__")

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

class C(A):
    def __init__(self):
        print("C.__init__")
        super().__init__()


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

If you issue D() at the REPL you get:

>>> D()
D.__init__
B.__init__
A.__init__
<D object at aac31180>

whereas cPython gives:

>>> D()
D.__init__
B.__init__
C.__init__
A.__init__
<__main__.D object at 0x7f40ecd1a358>
>>> 

A further issue with super() - with single inheritance - is this. Despite numerous successful uses I wrote one class where super() returned an empty string. Unfortunately any attempt to produce a test case failed. I appreciate that's a lousy bug report but I thought it worth mentioning if the code is being reviewed.

CANDIDATE · ISSUE

Multiple inheritance not working but also not throwing any error

openby mirkoopened 2020-05-26updated 2020-05-27
py-core

Multiple inheritance, according to docs, works different than in CPython - so far so good.

Example code:

class Base():
    def one(self):
        pass
    def two(self):
        pass

class X(Base):
    def one(self):
        print("one")

class Y(Base):
    def two(self):
        print("two")

class Test1(X, Y):
    def run(self):
        self.one()
        self.two()

class Test2(Y, X):
    def run(self):
        self.one()
        self.two()

print("Test1:")
Test1().run()
print("Test2:")
Test2().run()

Output CPython:

Test1:
one
two
Test2:
one
two

Output MicroPython:

Test1:
one
Test2:
two

So, yes, the documentation says something about MRO, but pretty vague (to me).
My understanding of the docs is: When there's a conflict, using above example as base, e.g. X and Y defining the same attribute, that there's no way of conflict resolution. Fair enough.

In above example, though, there's no conflict between the classes which derive from Base.
X and Y override /different/ attributes (X -> one / Y -> two), so the hierarchy is pretty clear and conflict resolution wouldn't be necessary.

If above is to be expected: why bother with multiple inheritance at all? As in: Why accepting multiple classes to derive from at all and not just throw an error indicating multiple inheritance is unsupported?

To me the current behaviour is very unpredictable, even when read the docs. It breaks existing code in a very subtle way. If possible, I'd favour a exception or alike here - even if it's just a syntax error.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied