[Python-Dev] Dinamically set call method (original) (raw)
Ethan Furman ethan at stoneleaf.us
Tue Nov 4 19:23:50 CET 2014
- Previous message: [Python-Dev] Dinamically set __call__ method
- Next message: [Python-Dev] Dinamically set __call__ method
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: [Python-Dev] Dinamically set __call__ method
- Next message: [Python-Dev] Dinamically set __call__ method
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]