Python's callables are objects that have a __call__
method.
They are most of the time functions, but might be class instances as well.
But it so happens that functions do have a __call__
method.
Therefore, a __call__
method has a __call__
method as well.
The following REPL session shows that we can chain the __call__
s:
>>> print
<built-in function print>
>>> print.__call__
<method-wrapper '__call__' of builtin_function_or_method object at 0x0000025E2D597F78>
>>> print.__call__.__call__
<method-wrapper '__call__' of method-wrapper object at 0x0000025E2F631438>
>>> print.__call__.__call__.__call__
<method-wrapper '__call__' of method-wrapper object at 0x0000025E2F5F85F8>
>>> print.__call__.__call__.__call__.__call__
<method-wrapper '__call__' of method-wrapper object at 0x0000025E2F725DA0>
... and so on. Remarkably, all these methods have different addresses. In addition, they all have the same behaviour:
>>> print("a")
a
>>> print.__call__("a")
a
>>> print.__call__.__call__("a")
a
>>> print.__call__.__call__.__call__("a")
So, when I write print("a")
, what is actually called?
Is it print
, or print.__call__
?
And what if I define a Foo
class with a __call__
method?
Furthermore, how is it possible that every __call__
method has its own different __call__
method?
Could it be that they are in fact created when I tried to access them?