Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80213 > unrolled thread

Re: [Python-Dev] Dinamically set __call__ method

Started byEthan Furman <ethan@stoneleaf.us>
First post2014-11-04 10:23 -0800
Last post2014-11-05 15:28 +1000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [Python-Dev] Dinamically set __call__ method Ethan Furman <ethan@stoneleaf.us> - 2014-11-04 10:23 -0800
    Re: [Python-Dev] Dinamically set __call__ method alex23 <wuwei23@gmail.com> - 2014-11-05 15:28 +1000

#80213 — Re: [Python-Dev] Dinamically set __call__ method

FromEthan Furman <ethan@stoneleaf.us>
Date2014-11-04 10:23 -0800
SubjectRe: [Python-Dev] Dinamically set __call__ method
Message-ID<mailman.15460.1415125440.18130.python-list@python.org>
This list is for the development _of_ Python, not development _with_ Python.

Try asking on Python List.

(forwarding...)

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.
> 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?

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:

   setattr(__self__.__class__, self.newcall)

--
~Ethan~

[toc] | [next] | [standalone]


#80245

Fromalex23 <wuwei23@gmail.com>
Date2014-11-05 15:28 +1000
Message-ID<m3ccik$r48$1@dont-email.me>
In reply to#80213
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.
>> 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".

Given that special methods can only be replaced on the class and not the 
instance, you could create a new version of the class within __init__ 
and assign it to the instance:

     class A:
         def __init__(self, call_method=None):
             if call_method:
		methods = {'__call__': call_method}
                 self.__class__ = type('Custom_A', (A,), )

         def __call__(self):
             return 'original'

     >> a = A()
     >> a()
     "old"
     >> b = A(lambda self: 'new')
     >> b()
     "new"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web