[Python-Dev] Dinamically set __call__ method

Roberto Martínez robertomartinezp at gmail.com
Tue Nov 4 17:52:39 CET 2014


Hi folks,

I am trying to replace dinamically the __call__ method of an object using
setattr.
Example:

$ cat testcall.py
class A:
    def __init__(self):
        setattr(self, '__call__', self.newcall)

    def __call__(self):
        print("OLD")

    def newcall(self):
        print("NEW")

a=A()
a()

I expect to get "NEW" instead of "OLD", but in Python 3.4 I get "OLD".

$ python2.7 testcall.py
NEW
$ python3.4 testcall.py
OLD

I have a few questions:

- Is this an expected behavior?
- Is possible to replace __call__ dinamically in Python 3? How?

Best regards,
Roberto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20141104/09b53446/attachment.html>


More information about the Python-Dev mailing list