472,958 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Callback scoping

Dan
So, I think I understand what python's scoping is doing in the
following situation:
>>x = [ lambda: ind for ind in range(10) ]
x
[<function <lambdaat 0x00BEC070>, <function <lambdaat 0x00BEC7F0>,
<function <lambdaat 0x00BECA70>, <function <lambdaat 0x00C1EBF0>,
<function <lambdaat 0x00C1EE30>, <function <lambdaat 0x00C228F0>,
<function <lambdaat 0x00C228B0>, <function <lambdaat 0x00C28730>,
<function <lambdaat 0x00C286F0>, <function <lambdaat 0x00C287F0>]
>>x[0]()
9
>>x[5]()
9
>>x[9]()
9
>>ind
9
>>ind = 2
x[0]()
2
>>>
But, I'm wondering what is the easiest (and/or most pythonic) way to
get the behavior I want? (If you haven't guessed, I want a list of (no
parameter) functions, each of which returns its index in the list.)

-Dan

Jul 5 '07 #1
2 984
On Thu, 05 Jul 2007 19:14:07 +0000, Dan wrote:
So, I think I understand what python's scoping is doing in the
following situation:
>>>x = [ lambda: ind for ind in range(10) ]

[…]

But, I'm wondering what is the easiest (and/or most pythonic) way to
get the behavior I want? (If you haven't guessed, I want a list of (no
parameter) functions, each of which returns its index in the list.)
Default arguments are evaluated when the function is defined:

In [15]: x = [lambda x=i: x for i in xrange(10)]

In [16]: x[0]()
Out[16]: 0

In [17]: x[5]()
Out[17]: 5

Ciao,
Marc 'BlackJack' Rintsch
Jul 5 '07 #2
Dan <th********@gmail.comwrote:
So, I think I understand what python's scoping is doing in the
following situation:
>x = [ lambda: ind for ind in range(10) ]

But, I'm wondering what is the easiest (and/or most pythonic) way to
get the behavior I want? (If you haven't guessed, I want a list of (no
parameter) functions, each of which returns its index in the list.)
This is the traditional way :-
>>x = [ lambda ind=ind: ind for ind in range(10) ]
x[0]()
0
>>x[2]()
2
>>x[9]()
9
>>>
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 5 '07 #3

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

Similar topics

8
by: Ian McMeans | last post by:
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...
2
by: David Stockwell | last post by:
Hi, Another of my crazy questions. I'm just in the process of learning so bear with me if you can. I actually ran it.... with two test cases TEST CASE 1: Say I have the following defined:...
2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
4
by: Joel Gordon | last post by:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i /...
2
by: Anand | last post by:
Hi Season Greetings, I'm trying to implement client call backs in my page. i'm using user controls (composite controls) and these are placed in my master page. I'm implementing...
9
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,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
3
by: SPECTACULAR | last post by:
Hi all. I have a question here.. what kind of scoping does C++ use? and what kind does Smalltalk use? I know smalltalk is a little bit old .. but any help would be appreciated.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.