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

Home Posts Topics Members FAQ

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(la mbda: 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 1009
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(la mbda: 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,it em) 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(la mbda: 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,it em) 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.w eb.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(la mbda: 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
3534
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 by pointer in C++, is there such way in Python? Thansk in advance. J.R.
18
1544
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 the notion of "anonymous functions" go - partly because I use them all the time. Of course, one can always create a named function. But there are a lot of cases, such as multimethods / generics and other scenarios where functions are treated...
5
2523
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
10870
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 http://www.artima.com/weblogs/viewpost.jsp?thread=147358
8
8131
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: self.highlight(x)) buttons.pack(side=LEFT) The buttons are correctly numbered 1 through 5, but no matter which
7
2427
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 I-combinator: a do-nothing function. The best I know of is: (lambda x: x), but it is not ideal. Consider a much-simplified example of such an iteration where sometimes you need to transform an item by some function, but most of the time you don't:
21
1860
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 following function? def addn(n): return lambda x,inc=n: x+inc if i do addn(5) it returns
11
1391
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 old one. i thought what happend inside a function stays inside a function meaning what comes out is independent of what comes in. Meaning if I want the list I send as a parameter to the function I have to do x = func(x) and not just func(x) and x...
11
1691
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 { static void Main()
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
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.