Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; '-for': 0.05; 'cpython': 0.05; 'subject:Python': 0.06; 'class,': 0.07; 'subject:method': 0.09; 'subject:set': 0.09; 'to:addr:pobox.com': 0.09; '~ethan~': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '2.7': 0.14; 'a()': 0.16; 'instance:': 0.16; 'python-dev,': 0.16; 'subject:Dev': 0.16; '\xa0def': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'subject:] ': 0.20; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'questions:': 0.24; 'replace': 0.24; 'skip:\xa0 20': 0.24; 'cc:2**0': 0.24; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'to:no real name:2**1': 0.29; 'message-id:@mail.gmail.com': 0.30; 'posting': 0.31; '3.x': 0.31; 'python2.7': 0.31; 'workaround': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'instances': 0.36; "didn't": 0.36; 'method': 0.36; 'possible': 0.36; 'detail': 0.37; 'list': 0.37; 'list.': 0.37; 'expected': 0.38; 'skip:& 10': 0.38; 'thank': 0.38; '8bit%:4': 0.38; 'nov': 0.38; 'pm,': 0.38; 'expect': 0.39; 'realize': 0.39; 'new': 0.61; 'skip:n 10': 0.64; 'different': 0.65; 'to:2**2': 0.65; 'to:addr:gmail.com': 0.65; '3.4': 0.84; 'ethan': 0.84; 'furman': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=zkSZO2WxpxBTHaVW1dm8KEwcJ2dZloUGiuZq/CutZpo=; b=QpYE9rxczVxNhPTT4Dm1/gSi8kTmn4P8VlzhH7iwtEWVDzjP8Cu+QksMa72tH0v4+Y 6hcGXGO3u4i8GF1MdoNIw3cbX0H0VlqOyaM+unA1JAcl6bGMDa/Ur12MpCad7Uf6lM2V T7eepss4rYNh9jcjBdDgYLCAuMnfuxag+WuMHdUdJlUw4JenKx7zw5UhNjgIqo8l6fE0 MJsBTk71EkSnV4aNUnrNSk6ujz/qe0Ha3uquLarOdGlpSKIrrO1z7WRPl5zVzMp9gFoK aQUoVStq8TxEIQoN/GLDHhGTxcVqNUc4T6M8pgtJtnRUrV1q8kF0eFjgJOzVnm6Xv8Nu LrHQ== X-Received: by 10.42.8.70 with SMTP id h6mr3682876ich.85.1415127718363; Tue, 04 Nov 2014 11:01:58 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <545919B6.7040007@stoneleaf.us> References: <545919B6.7040007@stoneleaf.us> From: =?ISO-8859-1?Q?Roberto_Mart=EDnez?= Date: Tue, 4 Nov 2014 20:01:38 +0100 Subject: Re: [Python-Dev] Dinamically set __call__ method To: Ethan Furman , njs@pobox.com, skip.montanaro@gmail.com Content-Type: multipart/alternative; boundary=001a11349c7af706d705070d18a4 Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 152 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415127726 news.xs4all.nl 2874 [2001:888:2000:d::a6]:54356 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80218 --001a11349c7af706d705070d18a4 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Yikes, I didn't realize the difference in inheritance. 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__. 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. Thank you all. Best regards, Roberto (Ethan, sorry for posting to python-dev, I thought that it was an implementation detail of CPython 3.X) On Tue, Nov 4, 2014 at 7:23 PM, Ethan Furman wrote: > 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=EDnez wrote: > >> >> I am trying to replace dinamically the __call__ method of an object usin= g >> 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=3DA() >> 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~ > --001a11349c7af706d705070d18a4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Yikes, I didn't realize the difference in inherit= ance.

The thing with this is tricky. I need the ch= ange in the instance, not in the class, because I have multiple instances a= nd all of them must have different implementations of __call__.=A0

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.

Thank y= ou all.

Best regards,
Roberto
=
(Ethan, sorry for posting to python-dev, I thought that it w= as an implementation detail of CPython 3.X)

On Tue, Nov 4, 2014 at 7:23 PM, Ethan= Furman <ethan@stoneleaf.us> wrote:
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=EDnez wrote:

I am trying to replace dinamically the __call__ method of an object using s= etattr.
Example:

$ cat testcall.py
class A:
=A0 =A0 =A0def __init__(self):
=A0 =A0 =A0 =A0 =A0setattr(self, '__call__', self.newcall)

=A0 =A0 =A0def __call__(self):
=A0 =A0 =A0 =A0 =A0print("OLD")

=A0 =A0 =A0def newcall(self):
=A0 =A0 =A0 =A0 =A0print("NEW")

a=3DA()
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'.=A0 W= hen you replace __call__ you need to replace it the class, not on the insta= nce:

=A0 setattr(__self__.__class__, self.newcall)

--
~Ethan~

--001a11349c7af706d705070d18a4--