How do I override a method object's __call__ method in Python?
python
Solution
This appears to be due to special-casing of function types in ceval.c, in `call_function`:
if (PyFunction_Check(func))
x = fast_function(func, pp_stack, n, na, nk);
else
x = do_call(func, pp_stack, na, nk);
I'd guess that this is probably for efficiency, since calling regular functions, and ignoring the `__call__` attribute, is by far the most common kind of calling that gets done.
Problem
Here is what I am working with so far ``` def f(n): return n f.__call__ = lambda n: n + 1 print f(2) #I expect an output of 3 but get an output of 2 ``` I am not interested in another way to achieve the desired output. Rather, for educational purposes, I would like to know why overriding the `__call__` as I have done, doesn't work as I expect.