[Python-Dev] Dinamically set __call__ method
ethan at stoneleaf.us
Tue Nov 4 19:23:50 CET 2014
This list is for the development _of_ Python, not development _with_ Python.
Try asking on Python List.
On 11/04/2014 08:52 AM, Roberto Martínez wrote:
> I am trying to replace dinamically the __call__ method of an object using setattr.
> $ cat testcall.py
> class A:
> def __init__(self):
> setattr(self, '__call__', self.newcall)
> def __call__(self):
> def newcall(self):
> I expect to get "NEW" instead of "OLD", but in Python 3.4 I get "OLD".
> $ python2.7 testcall.py
> $ python3.4 testcall.py
> I have a few questions:
> - Is this an expected behavior?
> - Is possible to replace __call__ dinamically in Python 3? How?
In 2.7 that would be a classic class, about which I know little.
In 3.x you have a new class, one which inherits from 'object'. When you replace __call__ you need to replace it the
class, not on the instance:
More information about the Python-list