How do I override a method object's __call__ method in Python? -


this question has answer here:

here working far

def f(n):     return n  f.__call__ = lambda n: n + 1  print f(2) #i expect output of 3 output of 2 

i not interested in way achieve desired output. rather, educational purposes, know why overriding __call__ have done, doesn't work expect.

this appears due 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 efficiency, since calling regular functions, , ignoring __call__ attribute, far common kind of calling gets done.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -