473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

parameters to lambda's executed at run time.

I'm trying to supply parameters to a function that is called at a
later time as in the code below:

llist = []

for item in range(5):
llist.append(lambda: func(item))

def func(item):
print item

for thing in llist:
thing()

which produces the result

IDLE 1.2.1
>>================================ RESTART ================================
<function <lambdaat 0xb716356c>
<function <lambdaat 0xb71635a4>
<function <lambdaat 0xb71635dc>
<function <lambdaat 0xb7163614>
<function <lambdaat 0xb716364c>
>>================================ RESTART ================================
4
4
4
4
4
>>>
How can one allocate a different parameter to each instance of the
function rather than all of them getting the final value of the loop?

Jun 27 '08 #1
4 998
wyleu wrote:
I'm trying to supply parameters to a function that is called at a
later time as in the code below:

llist = []

for item in range(5):
llist.append(lambda: func(item))

def func(item):
print item

for thing in llist:
thing()

which produces the result

IDLE 1.2.1
>>>================================ RESTART
================================
<function <lambdaat 0xb716356c>
<function <lambdaat 0xb71635a4>
<function <lambdaat 0xb71635dc>
<function <lambdaat 0xb7163614>
<function <lambdaat 0xb716364c>
>>>================================ RESTART
================================
4
4
4
4
4
>>>>

How can one allocate a different parameter to each instance of the
function rather than all of them getting the final value of the loop?
That's a FAQ. Python creates a closure for you that will retain the last
value bound. To prevent that, you need to create a named paramter like
this:

lambda item=item: func(item)

That will bind the current item value at the lambda creation time.

Diez
Jun 27 '08 #2
One way :
>>from functools import partial
def func(item) : print item
>>llist = [partial(func,item) for item in range(5)]
for thing in llist : thing()
0
1
2
3
4
wyleu wrote:
I'm trying to supply parameters to a function that is called at a
later time as in the code below:

llist = []

for item in range(5):
llist.append(lambda: func(item))

def func(item):
print item

for thing in llist:
thing()

which produces the result

IDLE 1.2.1
>>>================================ RESTART ================================
<function <lambdaat 0xb716356c>
<function <lambdaat 0xb71635a4>
<function <lambdaat 0xb71635dc>
<function <lambdaat 0xb7163614>
<function <lambdaat 0xb716364c>
>>>================================ RESTART ================================
4
4
4
4
4

How can one allocate a different parameter to each instance of the
function rather than all of them getting the final value of the loop?
Jun 27 '08 #3
Boris Borcic wrote:
One way :
>>from functools import partial
>>def func(item) : print item
>>llist = [partial(func,item) for item in range(5)]
>>for thing in llist : thing()

0
1
2
3
4
Another way:
class Func(object):
def __init__(self, item):
self.item = item
def __call__(self):
print self.item

llist = [Func(item) for item in range(5)]
for thing in llist: thing()

Jun 27 '08 #4
On May 6, 5:17*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
wyleu wrote:
I'm trying to supply parameters to a function that is called at a
later time as in the code below:
llist = []
for item in range(5):
* * llist.append(lambda: func(item))
def func(item):
* * print item
for thing in llist:
* * thing()
which produces the result
IDLE 1.2.1
>>================================ RESTART
================================
<function <lambdaat 0xb716356c>
<function <lambdaat 0xb71635a4>
<function <lambdaat 0xb71635dc>
<function <lambdaat 0xb7163614>
<function <lambdaat 0xb716364c>
>>================================ RESTART
================================
4
4
4
4
4
How can one allocate a different parameter to each instance of the
function rather than all of them getting the final value of the loop?

That's a FAQ. Python creates a closure for you that will retain the last
value bound. To prevent that, you need to create a named paramter like
this:

lambda item=item: func(item)

That will bind the current item value at the lambda creation time.

Diez- Hide quoted text -

- Show quoted text -
I am getting lambda creation-time bindings on 2.5.
>>g= [ lambda h= i: h for i in range( 10 ) ]
[ e( ) for e in g ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>g= [ lambda: i for i in range( 10 ) ]
[ e( ) for e in g ]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
Jun 27 '08 #5

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

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
18
by: talin at acm dot org | last post by:
I've been reading about how "lambda" is going away in Python 3000 (or at least, that's the stated intent), and while I agree for the most part with the reasoning, at the same time I'd be sad to see...
5
by: Max Rybinsky | last post by:
Hello! Please take a look at the example. >>> a = # Just a list of tuples >>> a Now i want to get a list of functions x*y/n, for each (x, y) in a:
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
8
by: Jay | last post by:
I'm having a problem using lambda to use a command with an argument for a button in Tkinter. buttons = range(5) for x in xrange(5): buttons = Button(frame, text=str(x+1), command=lambda:...
7
by: Deron Meranda | last post by:
This is an optimization problem, one that occurs deep inside a loop that gets executed thousands of times. I'm looking for something in Python which would act like an identity operator, or...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
11
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the...
11
by: puzzlecracker | last post by:
I am not understanding what's happening in the code below (from accelerated c#) -- I know what the outcome but not how .net does it: using System; using System.Linq; public class LambdaTest...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.