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?
Like methods on classes, the
__call__
attribute is a descriptor object defined on the type, bound to the object you looked it up on:The binding behaviour (through the
__get__
method) is how the resultingmethod-wrapper
instance knows to pass in theprint
object asself
, just like the instance gets passed into methods you defined on a custom Python class.So yes, that means they are created on demand, and thus will have a unique id. They are otherwise just more instances of the same type.