Using python3's super in a comprehension seems to always result in TypeError: super(type, obj): obj must be an instance or subtype of type (but using python 2's super does work as expected)
class A(object):
def __repr__(self):
return "hi!"
class B(A):
def __repr__(self):
return "".join(super().__repr__() for i in range(2))
repr(B())
#output: <repr(<__main__.B at 0x7f70cf36fcc0>) failed: TypeError: super(type, obj): obj must be an instance or subtype of type>
class C(A):
def __repr__(self):
s = ''
for i in range(4):
s += super().__repr__()
return s
repr(C())
#output: hi!hi!hi!hi!
class D(A):
def __repr__(self):
return "".join(super(D,self).__repr__() for i in range(4))
repr(D())
#output: hi!hi!hi!hi!
So, why does new super() fail in generator comprehensions?
Addendum:
In [28]: class E(A):
....: def __repr__(self):
....: def inner():
....: print(repr(__class__))
....: inner()
....: return ''
In [29]: repr(E())
<class '__main__.E'>
Out[29]: ''
In [30]: class F(A):
....: def __repr__(self):
....: return "".join([super().__repr__() for i in range(4)])
....:
In [31]: repr(F())
TypeError: super(type, obj): obj must be an instance or subtype of type
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire