Re: Dynamic methods and lambda functions

2009-01-29 Thread coutinhoti...@gmail.com
On Jan 28, 11:32 pm, "Gabriel Genellina" wrote: > En Wed, 28 Jan 2009 16:05:39 -0200, coutinhoti...@gmail.com   > escribió: > > >   I had the same problem myself. > >   Mark's detailed explanation really helped me understand. > > >   I ended up doing something like: > > The code doesn't work as-i

Re: Dynamic methods and lambda functions

2009-01-28 Thread Gabriel Genellina
En Wed, 28 Jan 2009 16:05:39 -0200, coutinhoti...@gmail.com escribió: I had the same problem myself. Mark's detailed explanation really helped me understand. I ended up doing something like: The code doesn't work as-is, could you please post a working version? Just for the record,

Re: Dynamic methods and lambda functions

2009-01-28 Thread coutinhoti...@gmail.com
Hi! I had the same problem myself. Mark's detailed explanation really helped me understand. I ended up doing something like: class A: def __init__(self): names = 'n1', 'n2' for n in names: setattr(self, "get%s" % n, self._createGetter(n)) def _createGetter(se

Re: Dynamic methods and lambda functions

2009-01-26 Thread Steve Holden
Mark Wooding wrote: > Steve Holden writes: > >> Mark Wooding wrote: >>> * Assignment stores a new (reference to a) value in the variable. >>> >>> * Binding modifies the mapping between names and variables. >>> >> I realise I have omitted what was doubtless intended to be explanatory >> detail

Re: Dynamic methods and lambda functions

2009-01-26 Thread Mark Wooding
Steve Holden writes: > Mark Wooding wrote: >> * Assignment stores a new (reference to a) value in the variable. >> >> * Binding modifies the mapping between names and variables. >> > I realise I have omitted what was doubtless intended to be explanatory > detail, but I am having trouble rec

Re: Dynamic methods and lambda functions

2009-01-26 Thread Kay Schluehr
On 26 Jan., 15:13, Steve Holden wrote: > Mark Wooding wrote: > > unine...@gmail.com writes: > [...] > > * Assignment stores a new (reference to a) value in the variable. > > > * Binding modifies the mapping between names and variables. > > I realise I have omitted what was doubtless intended t

Re: Dynamic methods and lambda functions

2009-01-26 Thread Steve Holden
Mark Wooding wrote: > unine...@gmail.com writes: [...] > * Assignment stores a new (reference to a) value in the variable. > > * Binding modifies the mapping between names and variables. > I realise I have omitted what was doubtless intended to be explanatory detail, but I am having trouble r

Re: Dynamic methods and lambda functions

2009-01-26 Thread Mark Wooding
Michael Torrie writes: > Basically, don't use a lambda. Create a real, local closure with a > nested def block. That way the closure is created every time the > parent function is called. Nope. I explained the real problem quite clearly, and it's to do with the difference between binding and

Re: Dynamic methods and lambda functions

2009-01-25 Thread Kay Schluehr
On 23 Jan., 13:28, unine...@gmail.com wrote: > Hi, > I want to add some properties dynamically to a class, and then add the > corresponding getter methods. Something resulting in this: > > class Person: > def Getname(self): > return self.__name > > def Getage(self): > return

Re: Dynamic methods and lambda functions

2009-01-24 Thread Michael Torrie
unine...@gmail.com wrote: > The attributes are right, but the getter are not working. The problem > is that the lambda function always execute the last parameter passed > for all instances of the methods. How could it be done the right way? Basically, don't use a lambda. Create a real, local clos

Re: Dynamic methods and lambda functions

2009-01-23 Thread Mark Wooding
unine...@gmail.com writes: > class Person: > def __init__(self): > for prop in props: > setattr(self, "__" + prop[0], prop[1]) > setattr(Person, "Get" + prop[0], lambda self: getattr > (self, "__" + prop[0])) [...] > The attributes are right, but the getter ar

Re: Dynamic methods and lambda functions

2009-01-23 Thread Brian Allen Vanderburg II
unine...@gmail.com wrote: class Person: def __init__(self): for prop in props: setattr(self, "__" + prop[0], prop[1]) setattr(Person, "Get" + prop[0], lambda self: getattr (self, "__" + prop[0])) I've had a similar problem here and here is best how I can ex

Re: Dynamic methods and lambda functions

2009-01-23 Thread Steven D'Aprano
On Fri, 23 Jan 2009 04:28:33 -0800, unineuro wrote: > Hi, > I want to add some properties dynamically to a class, and then add the > corresponding getter methods. Something resulting in this: > > class Person: > def Getname(self): > return self.__name > > def Getage(self): >

Dynamic methods and lambda functions

2009-01-23 Thread unineuro
Hi, I want to add some properties dynamically to a class, and then add the corresponding getter methods. Something resulting in this: class Person: def Getname(self): return self.__name def Getage(self): return self.__age I've implemented the next code, creating the prope