1. Error Code
Class A:
def func(x: int):
return x + 1
Class B(A):
def use_func():
a = [super().func(i) for i in range(3)]
print(a)
the original code is different and has been modified since posting it online could cause code leaks of the company I am working for.
Yet, I believe this code explains the prob perfect.
I got an error in this line:
a = [super().func(i) for i in range(3)]
2. The cuase, 에러 이유
List comprehension has its own scope in Python 3 while Python 2 does not. That is the key to resolve this error.
To get deeper, In Python 2, super() requires users to put 2 arguments, type and object. So, the code used to be like this:
// This code is in B class
// B is A's child class which self refers to
a = [super(B, self).func(i) for i in range(3)]
In Python 3, though we don't have to clarify the arguments anymore, super() still gets arguments internally. However, List comprehension doesn't have self in it. That code would've worked if i had used Python 2.
3. Fix the error
To fix this problem, code changes are needed. Below is the changed code.
Class A:
def func(x: int):
return x + 1
Class B(A):
def use_func():
a = []
for i in range(3):
a.append(super().func(i))
print(a)