Re: lambda functions

2009-09-01 Thread alex23
Pierre wrote: > I would like to know if it is possible to define a loop in a lambda > function It is if you can easily replace the for loop with a call to map(): >>> s_minus_1 = lambda s: map(lambda x: x-1, s) >>> test = range(1, 100, 10) >>> test [1, 11, 21, 31, 41, 51, 61, 71, 81, 91] >>>

Re: lambda functions

2009-08-31 Thread Rhodri James
On Mon, 31 Aug 2009 08:41:57 +0100, Pierre wrote: Hello, I would like to know if it is possible to define a loop in a lambda function How to manage the indents ? Example : s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s [index]-1 You can't use commands in a lambda

Re: lambda functions

2009-08-31 Thread Paul Rubin
Pierre writes: > s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s > [index]-1 What are you trying to do here anyway? That looks broken. Maybe you want the list.insert method. -- http://mail.python.org/mailman/listinfo/python-list

Re: lambda functions

2009-08-31 Thread Gabriel Genellina
En Mon, 31 Aug 2009 04:41:57 -0300, Pierre escribió: I would like to know if it is possible to define a loop in a lambda function How to manage the indents ? Example : s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s [index]-1 You can't. lambda is just a way to defin

Re: lambda functions

2009-08-31 Thread Chris Rebert
On Mon, Aug 31, 2009 at 12:41 AM, Pierre wrote: > Hello, > > I would like to know if it is possible to define a loop in a lambda > function Not possible. Lambdas can only contain a single expression. A loop is a block statement. Just use a named function instead. There's nothing that can be do

Re: lambda functions

2009-08-31 Thread Javier Collado
Hello, This page has some advice about how to avoid some of the lambda functions limitations: http://p-nand-q.com/python/stupid_lambda_tricks.html In particular, it suggests to use map function instead of for loops. Best regards, Javier 2009/8/31 Pierre : > Hello, > > I would lik

lambda functions

2009-08-31 Thread Pierre
Hello, I would like to know if it is possible to define a loop in a lambda function How to manage the indents ? Example : s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s [index]-1 Thanks ! -- http://mail.python.org/mailman/listinfo/python-list

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

Re: Getting a set of lambda functions

2008-05-25 Thread John Nagle
Martin Manns wrote: Hi, I try to get a set of lambda functions that allows me executing each function code exactly once. Therefore, I would like to modify the set function to compare the func_code properties (or the lambda functions to use this property for comparison). (The reason is that

Re: Getting a set of lambda functions

2008-05-25 Thread Terry Reedy
"Martin Manns" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | On Sun, 25 May 2008 14:39:28 -0700 (PDT) | [EMAIL PROTECTED] wrote: | | > This may have some bugs left, but it looks a bit better: | [...] | > self._hash = hash(self._func.func_code) ^ \ | >

Re: Getting a set of lambda functions

2008-05-25 Thread Terry Reedy
"I V" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: | > I try to get a set of lambda functions that allows me executing each I think it worth the reminder that Python has lambda *expressions* that

Re: Getting a set of lambda functions

2008-05-25 Thread Martin Manns
On Sun, 25 May 2008 14:39:28 -0700 (PDT) [EMAIL PROTECTED] wrote: > This may have some bugs left, but it looks a bit better: [...] > self._hash = hash(self._func.func_code) ^ \ > hash(tuple(signature[0]) + tuple(signature[1:3])) > def __eq__(self, other): >

Re: Getting a set of lambda functions

2008-05-25 Thread Denis Kasak
your spam functions, you'll get an "NameError: global name 'i' is not defined". Ah, the problem was in the subtle misunderstanding of the semantics of lambda functions on my part. It's much clearer now. Thanks. There are a couple of ways to slve your problem: (1) u

Re: Getting a set of lambda functions

2008-05-25 Thread Scott David Daniels
Denis Kasak wrote: ... spam = [] for i in range(10): ... spam.append(lambda: i) spam[0]() 9 spam[1]() 9 Manually creating the lambdas and appending them to a list works as expected, naturally; I don't see a good reason why it wouldn't work with a loop. Am I missing something? Yes, you a

Re: Getting a set of lambda functions

2008-05-25 Thread Denis Kasak
On Sun, May 25, 2008 at 1:43 PM, Martin Manns <[EMAIL PROTECTED]> wrote: > Hi, > > I try to get a set of lambda functions that allows me executing each > function code exactly once. Therefore, I would like to modify the set > function to compare the func_code properties (or t

Re: Getting a set of lambda functions

2008-05-25 Thread Denis Kasak
On Sun, May 25, 2008 at 1:43 PM, Martin Manns <[EMAIL PROTECTED]> wrote: > Hi, > > I try to get a set of lambda functions that allows me executing each > function code exactly once. Therefore, I would like to modify the set > function to compare the func_code properties (or t

Re: Getting a set of lambda functions

2008-05-25 Thread bearophileHUGS
This may have some bugs left, but it looks a bit better: from inspect import getargspec class HashableFunction(object): """Class that can be used to wrap functions, to allow their hashing, for example to create a set of unique functions. >>> func_strings = ['x', 'x+1', 'x+2', 'x']

Re: Getting a set of lambda functions

2008-05-25 Thread bearophileHUGS
I V: > You might instead want to >wrap the lambdas in an object that will do the comparison you want: This looks very nice, I haven't tried it yet, but if it works well then it may deserve to be stored in the cookbook, or better, it may become the built-in behavior of hashing functions. Bye, bear

Re: Getting a set of lambda functions

2008-05-25 Thread I V
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: > I try to get a set of lambda functions that allows me executing each > function code exactly once. Therefore, I would like to modify the set > function to compare the func_code properties (or the lambda functions to > use this

Re: Getting a set of lambda functions

2008-05-25 Thread Martin Manns
On Sun, 25 May 2008 12:14:25 + (UTC) Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: > > Maybe make a set of code objects? > > func_code_set = set([f.func_code for f in funclist]) > > funclist = [] > for fc in func_code_set: > f = lam

Re: Getting a set of lambda functions

2008-05-25 Thread Ivan Illarionov
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: > Hi, > > I try to get a set of lambda functions that allows me executing each > function code exactly once. Therefore, I would like to modify the set > function to compare the func_code properties (or the lambda function

Re: Getting a set of lambda functions

2008-05-25 Thread Ivan Illarionov
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: > Hi, > > I try to get a set of lambda functions that allows me executing each > function code exactly once. Therefore, I would like to modify the set > function to compare the func_code properties (or the lambda function

Getting a set of lambda functions

2008-05-25 Thread Martin Manns
Hi, I try to get a set of lambda functions that allows me executing each function code exactly once. Therefore, I would like to modify the set function to compare the func_code properties (or the lambda functions to use this property for comparison). (The reason is that the real function list

Re: lambda functions ?

2007-02-06 Thread Eduardo \"EdCrypt\" O. Padoan
> This means that "f" is not a pointer to make_incrementor but rather to > the internal (copied?) function. "returned" function isthe right here. As any returned object from a function. > > > This style is very common in Scheme programming so you might read a > > Scheme book if you want to underst

Re: lambda functions ?

2007-02-05 Thread Maxim Veksler
Wow, Thank you everyone for the help. I am amazed by the motivation people have on this list to help new comers. I hope that I will be able to contribute equally some day. On 05 Feb 2007 14:22:05 -0800, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote: > "Maxim Veksler" <[EMAIL PROTECTED]> write

Re: lambda functions ?

2007-02-05 Thread Toby A Inkster
Maxim Veksler wrote: > And what is the "f" object? An integer? a pointer? an Object? A function. -- Toby A Inkster BSc (Hons) ARCS Contact Me ~ http://tobyinkster.co.uk/contact Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux * = I'm getting there! -- http://mail.python.org/mai

Re: lambda functions ?

2007-02-05 Thread Bruno Desthuilliers
Maxim Veksler a écrit : > Hello, > I'm new on this list and in python. Welcome on board... > It seems python has some interesting concept of "ad hoc" function > which I'm trying to understand without much success. > > Take the following code for example: > > """ > def make_incrementor(n):

Re: lambda functions ?

2007-02-05 Thread Paul Rubin
"Maxim Veksler" <[EMAIL PROTECTED]> writes: > >>> def make_incrementor(n): > ... return lambda x: x + n Is the same as: def make_incrementor(n): def inner(x): return x + n return inner When you enter make_incrementor, it allocates a memory slot (normally we'd think of this as

Re: lambda functions ?

2007-02-05 Thread Don Morrison
Maybe you would like a generator: >>> def f(n): ... while True: ... n += 1 ... yield n ... >>> a = f(5) >>> >>> a.next() 6 >>> a.next() 7 >>> a.next() 8 >>> a.next() 9 >>> On 2/5/07, Maxim Veksler <[EMAIL PROTECTED]> wrote: > Hello, > I'm new on this list and in pytho

lambda functions ?

2007-02-05 Thread Maxim Veksler
Hello, I'm new on this list and in python. It seems python has some interesting concept of "ad hoc" function which I'm trying to understand without much success. Take the following code for example: """ >>> def make_incrementor(n): ... return lambda x: x + n ... >>> f = make_incrementor(42) >>>

Re: Use of lambda functions in OOP, any alternative?

2006-05-26 Thread Maric Michaud
Le Mercredi 24 Mai 2006 22:37, Scott David Daniels a écrit : >      class Base(object): >          def __init__(self, attr): >              self._attr = attr >          def getattr(self): >              return self._attr >          def attr(self): >              return self.getattr() >          att

Re: Use of lambda functions in OOP, any alternative?

2006-05-25 Thread Pablo
Oh! Thanx! Great! this is what i was looking for! :) Scott David Daniels ha escrito: > Pablo wrote: > > > Second solution: This is what i want, but... > > > > class Base(object): > > def __init__(self, attr): > > self._attr = attr > > def getattr(self): > > return self._at

Re: Use of lambda functions in OOP, any alternative?

2006-05-24 Thread Scott David Daniels
Pablo wrote: > Second solution: This is what i want, but... > > class Base(object): > def __init__(self, attr): > self._attr = attr > def getattr(self): > return self._attr > attr = property(fget=lambda self: self.getattr()) > > class Derived(Base): > def getattr(

Re: Use of lambda functions in OOP, any alternative?

2006-05-23 Thread Pablo
The reason i would like a different approach to the lambda function is just a question of personal taste... i dont really like it. thanx! -- http://mail.python.org/mailman/listinfo/python-list

Re: Use of lambda functions in OOP, any alternative?

2006-05-23 Thread Maric Michaud
Le Mardi 23 Mai 2006 15:55, Pablo a écrit : > Question: Isn't there an *alternative* way to do it without the lambda > function? No, it's good, why shouldn't you use a lambda function ? Note you can do something like this : class _virtualgetter : def __init__(self, name) : self._n =name

Re: Use of lambda functions in OOP, any alternative?

2006-05-23 Thread Pablo
Pablo ha escrito: > Hello all, sorry if this is a faq... > > Problem: The intended effect is to override the method 'getattr' in a > way that i dont need to override the property explicitly too. > > class Base(object): > def __init__(self, attr): > self._attr = attr > def getattr(s

Use of lambda functions in OOP, any alternative?

2006-05-23 Thread Pablo
Hello all, sorry if this is a faq... Problem: The intended effect is to override the method 'getattr' in a way that i dont need to override the property explicitly too. class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr

Re: lambda functions within list comprehensions

2005-10-30 Thread Max Rybinsky
Valid link in my previews message is http://mail.python.org/pipermail/python-dev/2005-September/056669.html Sorry. -- http://mail.python.org/mailman/listinfo/python-list

Re: lambda functions within list comprehensions

2005-10-30 Thread Max Rybinsky
OK. The thing i've got is an obscure semantic bug, occured because of my unawareness of the following Python "features": 1. (In major) http://mail.python.org/pipermail/python-dev/2005-September/056508.html 2. "late" bindings of the function's body Got to know! :) Thanks for your attention. -- h

Re: lambda functions within list comprehensions

2005-10-29 Thread Alex Martelli
Max Rybinsky <[EMAIL PROTECTED]> wrote: > Thank you for explanation, Alex. > It appears that almost every beginner to Python gets in trouble with > this ...feature. :) Almost every beginner to Python gets in trouble by expecting "do what I'm thinking of RIGHT NOW"-binding, which no language offer

Re: lambda functions within list comprehensions

2005-10-29 Thread Max Rybinsky
Thank you for explanation, Alex. It appears that almost every beginner to Python gets in trouble with this ...feature. :) -- http://mail.python.org/mailman/listinfo/python-list

Re: lambda functions within list comprehensions

2005-10-29 Thread Jean-Paul Calderone
On 29 Oct 2005 14:25:24 -0700, Max Rybinsky <[EMAIL PROTECTED]> wrote: >Hello! > >Please take a look at the example. > a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of tuples a >[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, >8), (9, 9)

Re: lambda functions within list comprehensions

2005-10-29 Thread Alex Martelli
Max Rybinsky <[EMAIL PROTECTED]> wrote: ... > >>> funcs = [lambda n: x * y / n for x, y in a] ... > It seems, all functions have x and y set to 9. > What's wrong with it? Is it a bug? It's known as *late binding*: names x and y are looked up when the lambda's body is executing, and at that t

lambda functions within list comprehensions

2005-10-29 Thread Max Rybinsky
Hello! Please take a look at the example. >>> a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of >>> tuples >>> a [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)] Now i want to get a list of functions x*y/n, for each (x, y) in a: >>> funcs