[issue13652] Creating lambda functions in a loop has unexpected results when resolving variables used as arguments

2011-12-22 Thread WIl Hall
New submission from WIl Hall w...@wilhall.com: When creating lambda functions in a loop, variables involved in the lambda statements appear to not resolve until the loop finishes. This results in all of the defined lambda functions using the same variable to resolve to the last value

[issue13652] Creating lambda functions in a loop has unexpected results when resolving variables used as arguments

2011-12-22 Thread Matthew Barnett
Matthew Barnett pyt...@mrabarnett.plus.com added the comment: That's not a bug. This might help to explain what's going on: What do (lambda) function closures capture in Python? http://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture-in-python -- nosy:

[issue13652] Creating lambda functions in a loop has unexpected results when resolving variables used as arguments

2011-12-22 Thread Benjamin Peterson
Changes by Benjamin Peterson benja...@python.org: -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13652 ___

[issue13652] Creating lambda functions in a loop has unexpected results when resolving variables used as arguments

2011-12-22 Thread WIl Hall
WIl Hall w...@wilhall.com added the comment: Makes sense. Misunderstanding on my part, then. Closing as invalid (not a bug). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13652 ___

Re: lambda functions

2009-09-01 Thread alex23
Pierre pierre.gaill...@gmail.com 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,

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: 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 pierre.gaill...@gmail.com: Hello

Re: lambda functions

2009-08-31 Thread Chris Rebert
On Mon, Aug 31, 2009 at 12:41 AM, Pierrepierre.gaill...@gmail.com 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

Re: lambda functions

2009-08-31 Thread Gabriel Genellina
En Mon, 31 Aug 2009 04:41:57 -0300, Pierre pierre.gaill...@gmail.com 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.

Re: lambda functions

2009-08-31 Thread Paul Rubin
Pierre pierre.gaill...@gmail.com 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 Rhodri James
On Mon, 31 Aug 2009 08:41:57 +0100, Pierre pierre.gaill...@gmail.com 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

[issue6740] Compounded expressions with lambda functions are evaluated incorrectly

2009-08-20 Thread Michal Vyskocil
New submission from Michal Vyskocil mvysko...@suse.cz: The compounded expressions with lambda functions are evaluated incorrectly. The simple expressions, or a named functions are evaluated good. The problem is only in the evaluation of compounded expressions. It seems that after evaluate

[issue6740] Compounded expressions with lambda functions are evaluated incorrectly

2009-08-20 Thread Eric Smith
Eric Smith e...@trueblade.com added the comment: This isn't the right forum to ask for help. You should try comp.lang.python, where someone would be happy to explain this to you. Having said that, here's the explanation: This is not a bug. Disregarding side effects, the expression: a = b or c

Re: Dynamic methods and lambda functions

2009-01-29 Thread coutinhoti...@gmail.com
On Jan 28, 11:32 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Wed, 28 Jan 2009 16:05:39 -0200, coutinhoti...@gmail.com   coutinhoti...@gmail.com escribió:   I had the same problem myself.   Mark's detailed explanation really helped me understand.   I ended up doing something

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

Re: Dynamic methods and lambda functions

2009-01-28 Thread Gabriel Genellina
En Wed, 28 Jan 2009 16:05:39 -0200, coutinhoti...@gmail.com 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?

Re: Dynamic methods and lambda functions

2009-01-26 Thread Mark Wooding
Michael Torrie torr...@gmail.com 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

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

Re: Dynamic methods and lambda functions

2009-01-26 Thread Kay Schluehr
On 26 Jan., 15:13, Steve Holden st...@holdenweb.com 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

Re: Dynamic methods and lambda functions

2009-01-26 Thread Mark Wooding
Steve Holden st...@holdenweb.com 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

Re: Dynamic methods and lambda functions

2009-01-26 Thread Steve Holden
Mark Wooding wrote: Steve Holden st...@holdenweb.com 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

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

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

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):

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 explain

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 are not

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: 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 functions to use this property

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 functions to use this property

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 = lambda x: x

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 property

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,

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 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 the lambda functions to use

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 the lambda functions to use

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

Re: Getting a set of lambda functions

2008-05-25 Thread Denis Kasak
: 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) use default args to do the binding at the function definition time. for i

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 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 result in function objects

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 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

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 understand it.

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) f(0) 42 f(1)

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 python. It seems python has

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 a stack

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): ... return

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! --

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] writes:

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()          attr =

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._attr attr =

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(self):

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: 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(self):

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
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

[ python-Bugs-1390991 ] lambda functions confused when mapped in dictionary object

2005-12-28 Thread SourceForge.net
thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Python 2.4 Status: Closed Resolution: Invalid Priority: 5 Submitted By: Samuel Hsiung (shsiung) Assigned to: Nobody/Anonymous (nobody) Summary: lambda functions

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. --

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

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 = [lambda

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 time

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)] Now i want to

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 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 offers: in