Python's magic method
Under the hood, all calls in Python use the same mechanism, and almost all arrive at the same C function in the CPython implementation. Whether an object is an instance of a class with a
__call__ method, a function (itself an object), or a builtin object, all calls (except for optimized special cases) arrive at the function
PyObject_Call. That C function gets the object's type from the
ob_type field of the object's
PyObject struct, and then from the type (another
PyObject struct) gets the
tp_call field, which is a function pointer. If
tp_call is not
NULL, it calls through that, with the args and kwargs structures that were also passed to
When a class defines a
__call__ method, that sets up the
tp_call field appropriately.
Here's an article explaining all of this in detail: Python internals: How callables work. It even lists and explains the entire
PyObject_Call function, which isn't very big. If you want to see that function in its native habitat, it's in Objects/abstract.c in the CPython repo.
Also relevant is this stackoverflow Q&A: What is a "callable" in Python?.