Imagine the following situation in python 3 (I'm using 3.6):
class T(object):
def __call__(self):
return 5
class U(T):
def __call__(self):
return 10 + super()()
U()()
This results in TypeError: 'super' object is not callable
.
Making it work requires U
to be defined as follows:
class U(T):
def __call__(self):
return 10 + super().__call__()
Why does the first version not work? I found this related question, but it doesn't answer this question. In fact, this question is also asked in a comment on the answer there.
Further reading about what super() actually returns gives me the impression that the proxy returned by super()
would have to implement __call__
and forward to the correct __call__
implementation.
From this answer, I get that the proxy object that is returned by super()
only has a __getattribute__
method that returns the correct function of whichever parent.
If I understand correctly, that means that the proxy would only need to implement the following thing to make super()()
work:
def __call__(self, *args, **kwargs):
try:
return self["__call__"](*args, **kwargs) # not sure if self is already bound to the method, if not that needs to be handled
except AttributeError as ae:
raise TypeError("Super is not callable") from ae
So why does it not do that?