4

What exactly happens when a function is called in Python? Does the interpreter call a __call__ method of an object? Then, why does the behaviour of function stay the same when I replace its __call__ method?

(I am working with Python 3)

>>> def foo(): return 1
>>> foo()
1
>>> foo.__call__()
1
>>> foo.__call__ = lambda: 2
>>> foo.__call__()
2
>>> foo()
1

Thank you.

R Bazhenov
  • 178
  • 8
  • 1
    Also see [here](http://stackoverflow.com/questions/34261111/patch-call-of-a-function) and [here](http://stackoverflow.com/questions/32855927/how-does-call-actually-work). – TigerhawkT3 Apr 14 '16 at 12:11
  • Thanks. Questions in your comments really pointed me in a right direction. – R Bazhenov Apr 14 '16 at 12:20
  • @TigerhawkT3 Do you, perhaps, know or can you point to why function 'instances' need `__call__` method if `__code__` is what gets executed by an actual call? – R Bazhenov Apr 14 '16 at 13:55

0 Answers0