A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2014-November/136894.html below:

[Python-Dev] Dinamically set __call__ method

[Python-Dev] Dinamically set __call__ method [Python-Dev] Dinamically set __call__ methodRoberto 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4