473,804 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scoping with lambda in loops

I was bitten by a bug today that depended on how lambda works. It took
me quite a while to realize what was going on.

First, I made multiple lambda functions inside a loop, each of which
depended on the current loop variable.
a = []
for index in range(5): a.append(lambda : index)
Now, see if you can guess what the output was for each of the
functions in the list a: a[0](), a[1](), a[2](), a[3](), a[4]()

I had expected it to be (0, 1, 2, 3, 4), but actually, it's:

(4, 4, 4, 4, 4)

This really surprised me. I guess what is happening is that each
lambda knows what the context of execution is where it was defined,
and doesn't actually evaluate until the function is called, and when
it does evaluate, it uses the current value of the variable. Is this
related to static scoping? A similar thing would happen if you defined
a nested function that used a variable declared in the outer function,
then changed that variable, and called the nested function.

Can someone recommend a way to code around this gotcha? I'm having
trouble. I want the functions created inside the loop to execute with
the value of the loop index at the moment when the function is made.
Jul 18 '05 #1
8 2630
On 16 Sep 2003 14:38:16 -0700, rumours say that im******@telus. net (Ian
McMeans) might have written:
First, I made multiple lambda functions inside a loop, each of which
depended on the current loop variable.
a = []
for index in range(5): a.append(lambda : index)

Now, see if you can guess what the output was for each of the
functions in the list a: a[0](), a[1](), a[2](), a[3](), a[4]()
I had expected it to be (0, 1, 2, 3, 4), but actually, it's:

(4, 4, 4, 4, 4)

This really surprised me. I guess what is happening is that each
lambda knows what the context of execution is where it was defined,
and doesn't actually evaluate until the function is called, and when
it does evaluate, it uses the current value of the variable. Is this
related to static scoping? A similar thing would happen if you defined
a nested function that used a variable declared in the outer function,
then changed that variable, and called the nested function.

Can someone recommend a way to code around this gotcha? I'm having
trouble. I want the functions created inside the loop to execute with
the value of the loop index at the moment when the function is made.


I think this is a FAQ (perhaps it was FAQ 6.10?), and you can find many
threads on the subject if you do a search on groups.google.c om.

The typical way to deal with this, IIRC, is to change your lambda
declaration into:
a.append(lambda index=index: index)


so that index gets evaluated at definition time.
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #2
Ian McMeans wrote:
a = []
for index in range(5): a.append(lambda : index) [...] Can someone recommend a way to code around this gotcha? I'm having
trouble. I want the functions created inside the loop to execute with
the value of the loop index at the moment when the function is made.


intuitively, i would think this should do the trick:
a = []
for index in range(5):

.... a.append(lambda x=index: x)

and testing shows that it does indeed.

but the reason why is rather vague to me, (i'm still rather new at this...)
so perhaps i should think a little more before trying to explain. (and i'm
sure someone else will come and do it better than i ever could.)

--
Joost Kremers
since when is vi an editor? a discussion on vi belongs in
comp.tools.unus able or something... ;-)
Jul 18 '05 #3
Ian> First, I made multiple lambda functions inside a loop, each of which
Ian> depended on the current loop variable.
a = []
for index in range(5):
a.append(lambda : index)

Ian> Now, see if you can guess what the output was for each of the
Ian> functions in the list a: a[0](), a[1](), a[2](), a[3](), a[4]()

Ian> I had expected it to be (0, 1, 2, 3, 4), but actually, it's:

Ian> (4, 4, 4, 4, 4)

Ian> This really surprised me.

Suppose you did it this way:

a = []
for index in range(5):
def foo():
return index
a.append(foo)

What result would you expect now, and why?

--
Andrew Koenig, ar*@acm.org
Jul 18 '05 #4
In article <7f************ **************@ posting.google. com>,
im******@telus. net (Ian McMeans) wrote:
First, I made multiple lambda functions inside a loop, each of which
depended on the current loop variable.
a = []
for index in range(5): a.append(lambda : index)
Now, see if you can guess what the output was for each of the
functions in the list a: a[0](), a[1](), a[2](), a[3](), a[4]()

I had expected it to be (0, 1, 2, 3, 4), but actually, it's:

(4, 4, 4, 4, 4)

This really surprised me. I guess what is happening is that each
lambda knows what the context of execution is where it was defined,
and doesn't actually evaluate until the function is called, and when
it does evaluate, it uses the current value of the variable. Is this
related to static scoping?


It's related to closures. If you're using lambda, you're probably a
lisp programmer, and should know all about closures. Creating a
function object with def or lambda, within an outer function scope,
creates a closure for that outer function call. The inner function's
accesses to variables from the outer function will return the
most-recently-updated binding from the closure. If you call the outer
function again, you will get a different unrelated closure.

If you the inner function to have its own local variable that stores
some expression value as it existed at the creation time of the inner
function, rather than re-evaluating the expression whenever the inner
function is called, the standard way is to use a defaulted keyword
parameter:

a = []
for index in range(5):
a.append(lambda index=index: index)

or maybe more concisely

a = [lambda index=index: index for index in range(5)]

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #5
> a = []
for index in range(5):
a.append(lambda index=index: index)

or maybe more concisely

a = [lambda index=index: index for index in range(5)]


You know how Python is supposed to be executable pseudocode? Well that
stuff is farking ugly. If I handed pseudocode like that into any TA in one
of my classes, I'd be toast. Is there any way to do that in a legible
manner?
Jul 18 '05 #6
"martin z" <px**@hotmail.c om> wrote in message
news:Kv******** **********@news 04.bloor.is.net .cable.rogers.c om...
a = []
for index in range(5):
a.append(lambda index=index: index)

or maybe more concisely

a = [lambda index=index: index for index in range(5)]
You know how Python is supposed to be executable pseudocode? Well that
stuff is farking ugly. If I handed pseudocode like that into any TA in

one of my classes, I'd be toast. Is there any way to do that in a legible
manner?


The following reads pretty well to me:
produce_value = lambda value: lambda: value
a = [produce_value(i ndex) for index in range(5)]
a[3]()

3

Dave

Jul 18 '05 #7
In article <Kv************ ******@news04.b loor.is.net.cab le.rogers.com>,
"martin z" <px**@hotmail.c om> wrote:
a = []
for index in range(5):
a.append(lambda index=index: index)

or maybe more concisely

a = [lambda index=index: index for index in range(5)]


You know how Python is supposed to be executable pseudocode? Well that
stuff is farking ugly. If I handed pseudocode like that into any TA in one
of my classes, I'd be toast. Is there any way to do that in a legible
manner?


How about this:

def makefunction(x) :
def thefunction():
return x
return thefunction

a = map(makefunctio n, range(5))

The identifiers are still a little uninformative, but it's hard to do
better without more information from the original poster...

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #8
im******@telus. net (Ian McMeans) writes:
a = []
for index in range(5): a.append(lambda : index)
Now, see if you can guess what the output was for each of the
functions in the list a: a[0](), a[1](), a[2](), a[3](), a[4]()
I had expected it to be (0, 1, 2, 3, 4), but actually, it's:

(4, 4, 4, 4, 4)

This really surprised me. I guess what is happening is that each
lambda knows what the context of execution is where it was defined,
and doesn't actually evaluate until the function is called, and when
it does evaluate, it uses the current value of the variable. Is this
related to static scoping?


It's related to _lexical_ scoping. It is called a lexical closure.

As of the time when nested scopes were introduced into Python,
whenever a name is referenced, it is first sought in the local lexical
scope (ie, the bit of text in the local function body); if it is not
found there, it is sought in the closest enclosing lexical scope (the
text of any enclosing functions), and so on; when you run out of
enclosing functions you try global, then builtin scope.

There is no "index" variable in your lambda's local scope, so it has
to resort to using the one in the global scope ... which, by the time
you get around to calling your functions, has been set to 4.
A similar thing would happen if you defined a nested function that
used a variable declared in the outer function, then changed that
variable, and called the nested function.
Yup.
Can someone recommend a way to code around this gotcha?


Make your own local binding. Function call parameters make local
bindings. So, within a lambda, you can achieve this by using a keyword
argument.

lambda index=index:ind ex (or lambda i=index:i)

Now there is a local index and a global index, so the lambda uses the
local one. Because the default value is evaluated at the time the
lambda expression is evaluated, each local index has a value
corresponding to whatever the global index had at the time the lambda
was evaluated.

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2147
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that may be of some interest. To refresh your memory, I report here some quotation from that thread. Jacek Generowicz: > I sumbit to you that read-only closures are rare in Python because > they are a recent addition to the language.
9
1637
by: Darabos Daniel | last post by:
Hi! I was doing something like this: >>> def p( x ): .... print x .... >>> l = >>> for i in range( 5 ): .... l.append( lambda: p( i ) )
7
1582
by: Philip Smith | last post by:
I've read with interest the continuing debate about 'lambda' and its place in Python. Just to say that personally I think its an elegant and useful construct for many types of programming task (particularly number theory/artificial intelligence/genetic algorithms) I can't think why anyone would be proposing to do away with it. Sometimes an anonymous function is just what you need and surely it just reflects the python philosophy of...
181
8932
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
30
2181
by: Mike Meyer | last post by:
I know, lambda bashing (and defending) in the group is one of the most popular ways to avoid writing code. However, while staring at some Oz code, I noticed a feature that would seem to make both groups happy - if we can figure out how to avoid the ugly syntax. This proposal does away with the well-known/obscure "lambda" keyword. It gives those who want a more functional lambda what they want. It doesn't add any new keywords. It doesn't...
9
1932
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13, in ? A() File "x1x2.py", line 11, in A
26
2736
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of locally-scoped-together code, in Ruby a "block" defines a closure (anonymous function). To avoid confusion let's call them Ruby block-closures. I see them as just a syntax for defining
2
1014
by: Dan | last post by:
So, I think I understand what python's scoping is doing in the following situation: 9 9 9 9 2 But, I'm wondering what is the easiest (and/or most pythonic) way to
2
1281
by: Joshua Kugler | last post by:
I am trying to use lamdba to generate some functions, and it is not working the way I'd expect. The code is below, followed by the results I'm getting. More comments below that. patterns = ( ('$', '$','es'), ('h$', '$', 'es'), ('y$', 'y$', 'ies'), ('$', '$', 's'), )
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.