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


Groups > comp.lang.python > #80213

Re: [Python-Dev] Dinamically set __call__ method

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'example:': 0.03; 'subject:: [': 0.04; 'subject:Python': 0.06; 'class,': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'subject:method': 0.09; 'subject:set': 0.09; '~ethan~': 0.09; 'python': 0.11; 'def': 0.12; '2.7': 0.14; 'a()': 0.16; 'instance:': 0.16; 'subject:Dev': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'subject:] ': 0.20; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'header:User-Agent:1': 0.23; 'questions:': 0.24; 'replace': 0.24; 'cc:no real name:2**0': 0.24; 'asking': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; '3.x': 0.31; 'python2.7': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'method': 0.36; 'possible': 0.36; 'list': 0.37; 'list.': 0.37; 'expected': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'new': 0.61; 'received:173': 0.61; 'skip:n 10': 0.64; '3.4': 0.84
Date Tue, 04 Nov 2014 10:23:50 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To Python <python-list@python.org>
Subject Re: [Python-Dev] Dinamically set __call__ method
References <CAF0TK51TKs+tw=u_rzrWVeSsDK7cOBt1Gcvq_izLfMObuDYNig@mail.gmail.com>
In-Reply-To <CAF0TK51TKs+tw=u_rzrWVeSsDK7cOBt1Gcvq_izLfMObuDYNig@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
Cc robertomartinezp@gmail.com, Python Dev <Python-Dev@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15460.1415125440.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415125440 news.xs4all.nl 2894 [2001:888:2000:d::a6]:51411
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80213

Show key headers only | View raw


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~

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

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

csiph-web