← index #2028Issue #3205
Duplicate · high · value 1.757
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

In case of multiple inheritance, super() might not find some parent methods

closedby dvrhaxopened 2017-07-09updated 2017-08-23

Discovered this issue while porting over PyMySQL to micropython both super() calling methods shown produce the same results

class a():
    def doSomething(self):
        print('Something done.')

class aMixin():
    def doSomething(self):
        #super(aMixin, self).doSomething()
        super().doSomething()
        print('Something Else.')

class b(aMixin, a):
    """Super Test Case"""

print('Class a:')
aa = a()
aa.doSomething()

print('Class b:')
bb = b()
bb.doSomething()

CPython prints:

Class a:
Something done.
Class b:
Something done.
Something Else.

However MicroPython produces the following error:

Class a:
Something done.
Class b:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "superTest.py", line 19, in <module>
File "superTest.py", line 7, in doSomething
AttributeError: 'super' object has no attribute 'doSomething'

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