I'm using Python 3.7, and I get an error in the code below. It looks like the super()
keyword doesn't work properly with list comprehensions. I want to know the cause.
class A(object):
def hello(self):
return 1
class B(A):
def __init__(self):
why_error = [
super().hello() for i in range(2) for j in range(2)
]
print(why_error)
class C(A):
def __init__(self):
why_no_error = [
super().hello(),
super().hello(),
super().hello(),
super().hello()
]
print(why_no_error)
c = C()
b = B()
And the execution result is as follows.
Traceback (most recent call last):
File "test.py", line 23, in <module>
b = B()
File "test.py", line 8, in __init__
super().hello() for i in range(2) for j in range(2)
File "test.py", line 8, in <listcomp>
super().hello() for i in range(2) for j in range(2)
TypeError: super(type, obj): obj must be an instance or subtype of type