Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 22nd, 2004, 05:40 AM
George Sakkis George Sakkis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 George Sakkis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Making closures in loop ?

Hi there,

I want to create a list of functions with something like:

closures = [lambda x: i*x for i in range(10)]

so that
closures[0] = 0*x
closures[1] = 1*x
closures[2] = 2*x
...
closures[9] = 9*x

For some reason, all elements seem to bind the last value of i, that is closures[j] = 9*x for all j, not only 9. Any ideas of why does this happen, and how should I make it work as intended ?

George

Reply With Quote
  #2  
Old February 22nd, 2004, 05:47 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
That is how it is bound.

Check what jeremy has got to say.

Reply With Quote
  #3  
Old February 22nd, 2004, 06:10 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,196 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 42 m 7 sec
Reputation Power: 252
You can get it to work the way you want by passing in i as a default parameter, i.e.:

Code:
>>> closures = [lambda x, i=i: i*x for i in range(10)]
>>> closures[1](2)
2
>>> closures[4](2)
8


This was the standard way to simulate closures in Python prior to 2.1, when it did not have the new scoping rules.

Dave - The Developers' Coach

Reply With Quote
  #4  
Old February 22nd, 2004, 11:28 AM
George Sakkis George Sakkis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 George Sakkis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I was sure there was a way around it; thanks a lot Dave !

George

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Making closures in loop ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway