I need to change the behavior of the __call__
method of a given object. The naive approach would be something like:
class A(object):
def __call__(self):
return 1
def new_call():
return 42
a = A()
a.__call__ = new_call
Why is it not the output of a()
42? Is there a workaround I can exploit to achieve the same effect? (without using the class)
============================ EDIT =================================
For the records, the short answer is no. Python calls the "special methods" like __call_
directly on the class and not on the instance, therefore if you need to change the method, you need to change it on the class itself.