Re: [Python-Dev] Dinamically set __call__ method

2014-11-13 Thread Lie Ryan
On 05/11/14 06:15, Roberto Martínez wrote: 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__. Why not just use functions with closure if that's what you need?

Re: [Python-Dev] Dinamically set __call__ method

2014-11-13 Thread Fabio Zadrozny
On Thu, Nov 13, 2014 at 2:20 AM, Gregory Ewing wrote: > Fabio Zadrozny wrote: > >> can someone from python-dev give some background of why that's the way it >> is? >> > > It's because, with new-style classes, a class is also an > instance (of class "type" or a subclass thereof). So > without that

Re: [Python-Dev] Dinamically set __call__ method

2014-11-12 Thread Gregory Ewing
Fabio Zadrozny wrote: can someone from python-dev give some background of why that's the way it is? It's because, with new-style classes, a class is also an instance (of class "type" or a subclass thereof). So without that rule, it would be ambiguous whether a dunder method applied to instances

Re: [Python-Dev] Dinamically set __call__ method

2014-11-12 Thread Ian Kelly
On Wed, Nov 12, 2014 at 8:33 AM, Fabio Zadrozny wrote: > As a reference, I recently found a blog post related to that: > http://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-to-see/ (the Slots > part comments on that). > > It does seem a bit counter-intuitive that this happens the way it does

Re: [Python-Dev] Dinamically set __call__ method

2014-11-12 Thread Fabio Zadrozny
On Tue, Nov 11, 2014 at 5:43 AM, Ian Kelly wrote: > On Sat, Nov 8, 2014 at 3:31 PM, Gregory Ewing > wrote: > > (BTW, I'm actually surprised that this technique makes c callable. > > There must be more going on that just "look up __call__ in the class > > object", because evaluating C.__call__ ju

Re: [Python-Dev] Dinamically set __call__ method

2014-11-10 Thread Ian Kelly
On Sat, Nov 8, 2014 at 3:31 PM, Gregory Ewing wrote: > (BTW, I'm actually surprised that this technique makes c callable. > There must be more going on that just "look up __call__ in the class > object", because evaluating C.__call__ just returns the descriptor > and doesn't invoking the descripto

Re: [Python-Dev] Dinamically set __call__ method

2014-11-09 Thread Ethan Furman
On 11/09/2014 03:38 AM, Gregory Ewing wrote: Ethan Furman wrote: And the thing going on is the normal python behavior (in __getattribute__, I believe) of examining the returned attribute to see if it is a descriptor, and if so invoking it. Only if you look it up through the instance, though.

Re: [Python-Dev] Dinamically set __call__ method

2014-11-09 Thread Steven D'Aprano
Gregory Ewing wrote: > Ethan Furman wrote: >> And the thing going on is the normal python behavior (in >> __getattribute__, I believe) of examining the returned attribute to see >> if it is a descriptor, and if so invoking it. > > Only if you look it up through the instance, though. > Normally, i

Re: [Python-Dev] Dinamically set __call__ method

2014-11-09 Thread Gregory Ewing
Ethan Furman wrote: And the thing going on is the normal python behavior (in __getattribute__, I believe) of examining the returned attribute to see if it is a descriptor, and if so invoking it. Only if you look it up through the instance, though. Normally, if you look up an attribute on a cla

Re: [Python-Dev] Dinamically set __call__ method

2014-11-08 Thread Ethan Furman
On 11/08/2014 02:31 PM, Gregory Ewing wrote: Seems to depend on how you get hold of the object you're inspecting the signature of. I did an experiment: class C(object): @property def __call__(self): return self.call def f(x, y): print("Called f with %s, %s" % (x, y))

Re: [Python-Dev] Dinamically set __call__ method

2014-11-08 Thread Gregory Ewing
Ethan Furman wrote: On 11/06/2014 10:59 PM, dieter wrote: A possibility to get the original approach implemented looks like: make "__call__" a descriptor on the class which looks up the real method on the instance. This still wouldn't get the signatrue correct, though. Why not? O

Re: [Python-Dev] Dinamically set __call__ method

2014-11-08 Thread Ethan Furman
On 11/07/2014 10:50 PM, dieter wrote: Ethan Furman writes: On 11/06/2014 10:59 PM, dieter wrote: John Ladasky writes: On Tuesday, November 4, 2014 11:12:31 AM UTC-8, Ethan Furman wrote: If you really absolutely positively have to have the signature be correct for each instance, you may to

Re: [Python-Dev] Dinamically set __call__ method

2014-11-07 Thread dieter
Ethan Furman writes: > On 11/06/2014 10:59 PM, dieter wrote: >> John Ladasky writes: >>> On Tuesday, November 4, 2014 11:12:31 AM UTC-8, Ethan Furman wrote: >>> If you really absolutely positively have to have the signature be correct for each instance, you may to either look at a

Re: [Python-Dev] Dinamically set __call__ method

2014-11-07 Thread Ethan Furman
On 11/06/2014 10:59 PM, dieter wrote: John Ladasky writes: On Tuesday, November 4, 2014 11:12:31 AM UTC-8, Ethan Furman wrote: If you really absolutely positively have to have the signature be correct for each instance, you may to either look at a function creating factory, a class creating f

Re: [Python-Dev] Dinamically set __call__ method

2014-11-06 Thread dieter
John Ladasky writes: > On Tuesday, November 4, 2014 11:12:31 AM UTC-8, Ethan Furman wrote: > >> If you really absolutely positively have to have the signature be correct >> for each instance, you may to either look at a >> function creating factory, a class creating factory, or a meta-class. >

Re: [Python-Dev] Dinamically set __call__ method

2014-11-06 Thread Steven D'Aprano
Roberto Martínez wrote: > 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 ca

Re: [Python-Dev] Dinamically set __call__ method

2014-11-06 Thread John Ladasky
On Tuesday, November 4, 2014 11:12:31 AM UTC-8, Ethan Furman wrote: > If you really absolutely positively have to have the signature be correct for > each instance, you may to either look at a > function creating factory, a class creating factory, or a meta-class. +1. Overriding __call__() wit

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread alex23
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") d

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Ethan Furman
On 11/04/2014 11:23 AM, Nathaniel Smith wrote: (Or alternatively I guess you could go all in: Iä! Iä! Metaclasses Fhtagn!) Metaclasses aren't that bad! I've written one. And the dizzy spells are getting better! -- ~Ethan~ -- https://mail.python.org/mailman/listinfo/python-list

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Nathaniel Smith
On Tue, Nov 4, 2014 at 7:15 PM, Roberto Martínez wrote: > > > On Tue, Nov 4, 2014 at 8:06 PM, Skip Montanaro > wrote: >> >> >> On Tue, Nov 4, 2014 at 1:01 PM, Roberto Martínez >> wrote: >>> >>> The workaround of calling a different method inside __call__ is not valid >>> for my case because I wa

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Roberto Martínez
On Tue, Nov 4, 2014 at 8:06 PM, Skip Montanaro wrote: > > On Tue, Nov 4, 2014 at 1:01 PM, Roberto Martínez < > robertomartin...@gmail.com> wrote: > >> 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

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Ethan Furman
On 11/04/2014 11:01 AM, Roberto Martínez wrote: (Ethan, sorry for posting to python-dev, I thought that it was an implementation detail of CPython 3.X) No worries. It's good practice to post here first, just in case. ;) -- ~Ethan~ -- https://mail.python.org/mailman/listinfo/python-list

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Ethan Furman
On 11/04/2014 11:01 AM, Roberto Martínez wrote: 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 work

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Skip Montanaro
On Tue, Nov 4, 2014 at 1:01 PM, Roberto Martínez wrote: > 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. You could define __call__ like so: def __call__(self, *a

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Roberto Martínez
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_

Re: [Python-Dev] Dinamically set __call__ method

2014-11-04 Thread Ethan Furman
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: