<div dir="ltr"><div>Yikes, I didn't realize the difference in inheritance.</div><div><br></div><div>The thing with this is tricky. I need the change in the instance, not in the class, because I have multiple instances and all of them must have different implementations of __call__. </div><div><br></div><div>The workaround of calling a different method inside __call__ is not valid for my case because I want to change the *signature* of the function also -for introspection reasons.</div><div><br></div><div>Thank you all.</div><div><br></div><div>Best regards,</div><div>Roberto</div><div><br></div><div>(Ethan, sorry for posting to python-dev, I thought that it was an implementation detail of CPython 3.X)</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 4, 2014 at 7:23 PM, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This list is for the development _of_ Python, not development _with_ Python.<br>
<br>
Try asking on Python List.<br>
<br>
(forwarding...)<div><div class="h5"><br>
<br>
On 11/04/2014 08:52 AM, Roberto Martínez wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I am trying to replace dinamically the __call__ method of an object using setattr.<br>
Example:<br>
<br>
$ cat testcall.py<br>
class A:<br>
     def __init__(self):<br>
         setattr(self, '__call__', self.newcall)<br>
<br>
     def __call__(self):<br>
         print("OLD")<br>
<br>
     def newcall(self):<br>
         print("NEW")<br>
<br>
a=A()<br>
a()<br>
<br>
I expect to get "NEW" instead of "OLD", but in Python 3.4 I get "OLD".<br>
<br>
$ python2.7 testcall.py<br>
NEW<br>
$ python3.4 testcall.py<br>
OLD<br>
<br>
I have a few questions:<br>
<br>
- Is this an expected behavior?<br>
- Is possible to replace __call__ dinamically in Python 3? How?<br>
</blockquote>
<br></div></div>
In 2.7 that would be a classic class, about which I know little.<br>
<br>
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:<br>
<br>
  setattr(__self__.__class__, self.newcall)<br>
<br>
--<br>
~Ethan~<br>
</blockquote></div><br></div>