473,382 Members | 1,313 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,382 software developers and data experts.

why is this different?

Hello snakes :)

In [38]: f = [lambda:i for i in range(10)]
In [39]: ff = map(lambda i: lambda : i, range(10))
In [40]: f[0]()
Out[40]: 9
In [41]: f[1]()
Out[41]: 9
In [42]: ff[0]()
Out[42]: 0
In [43]: ff[1]()
Out[43]: 1

I don't understand why in the first case f[for all i in 0..9]==9
what is different from (more usefull)

In [44]: f = ["%i" % i for i in range(10)]
In [45]: f
Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
doing it like this works again

In [54]: def d(x):
....: return lambda:x
....:

In [55]: f = [d(i) for i in range(10)]
In [56]: f[0]()
Out[56]: 0
In [57]: f[1]()
Out[57]: 1

in a C programmer sence I would say there seems to be no "sequence
point" which would separate "now" from "next"

Regards, Daniel
Dec 8 '06 #1
4 1042
On 7 dic, 22:53, Schüle Daniel <u...@rz.uni-karlsruhe.dewrote:
In [38]: f = [lambda:i for i in range(10)]
In [39]: ff = map(lambda i: lambda : i, range(10))
In [40]: f[0]()
Out[40]: 9
In [41]: f[1]()
Out[41]: 9
In [42]: ff[0]()
Out[42]: 0
In [43]: ff[1]()
Out[43]: 1

I don't understand why in the first case f[for all i in 0..9]==9
In the first case, i is a free variable. That means that Python will
get it from other place (the global namespace, likely [surely?])
>>f=[lambda:i for i in range(10)]
f[0]()
9
>>i=123
f[0]()
123
>>print f[0].func_closure
None

In the second case, the inner i is a free variable, but local to its
enclosing scope (outer lambda). It's a closure:
>>ff = map(lambda i: lambda : i, range(10))
ff[4]()
4
>>ff[4].func_closure
(<cell at 0x00ACEA90: int object at 0x00995344>,)
>>i=321
ff[4]()
4
what is different from (more usefull)

In [44]: f = ["%i" % i for i in range(10)]
In [45]: f
Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
This is a simple expression evaluated at each iteration
doing it like this works again

In [54]: def d(x):
....: return lambda:x
....:
x inside lambda is a free variable, but since it's local to d, this
becomes a closure:
>>d(1)
<function <lambdaat 0x00A35EF0>
>>d(1).func_closure
(<cell at 0x00A2A4F0: int object at 0x00995338>,)
In [55]: f = [d(i) for i in range(10)]
In [56]: f[0]()
Out[56]: 0
In [57]: f[1]()
Out[57]: 1
This is similar to the ff example above
in a C programmer sence I would say there seems to be no "sequence
point" which would separate "now" from "next"
No, the problem is that C has no way to express a closure; this is a
functional concept absolutely extraneous to the C language.

--
Gabriel Genellina

Dec 8 '06 #2
Gabriel Genellina schrieb:
Gabriel Genellina schrieb:
On 7 dic, 22:53, Schüle Daniel <u...@rz.uni-karlsruhe.dewrote:
>In [38]: f = [lambda:i for i in range(10)]
In [39]: ff = map(lambda i: lambda : i, range(10))
In [40]: f[0]()
Out[40]: 9
In [41]: f[1]()
Out[41]: 9
In [42]: ff[0]()
Out[42]: 0
In [43]: ff[1]()
Out[43]: 1

I don't understand why in the first case f[for all i in 0..9]==9

In the first case, i is a free variable. That means that Python will
get it from other place (the global namespace, likely [surely?])
>>>f=[lambda:i for i in range(10)]
f[0]()
9
>>>i=123
f[0]()
123
>>>print f[0].func_closure
None

In the second case, the inner i is a free variable, but local to its
enclosing scope (outer lambda). It's a closure:
>>>ff = map(lambda i: lambda : i, range(10))
ff[4]()
4
>>>ff[4].func_closure
(<cell at 0x00ACEA90: int object at 0x00995344>,)
>>>i=321
ff[4]()
4
>what is different from (more usefull)

In [44]: f = ["%i" % i for i in range(10)]
In [45]: f
Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

This is a simple expression evaluated at each iteration
>doing it like this works again

In [54]: def d(x):
....: return lambda:x
....:
x inside lambda is a free variable, but since it's local to d, this
becomes a closure:
>>>d(1)
<function <lambdaat 0x00A35EF0>
>>>d(1).func_closure
(<cell at 0x00A2A4F0: int object at 0x00995338>,)
>In [55]: f = [d(i) for i in range(10)]
In [56]: f[0]()
Out[56]: 0
In [57]: f[1]()
Out[57]: 1
This is similar to the ff example above
>in a C programmer sence I would say there seems to be no "sequence
point" which would separate "now" from "next"

No, the problem is that C has no way to express a closure; this is a
functional concept absolutely extraneous to the C language.
On 7 dic, 22:53, Schüle Daniel <u...@rz.uni-karlsruhe.dewrote:
>In [38]: f = [lambda:i for i in range(10)]
In [39]: ff = map(lambda i: lambda : i, range(10))
In [40]: f[0]()
Out[40]: 9
In [41]: f[1]()
Out[41]: 9
In [42]: ff[0]()
Out[42]: 0
In [43]: ff[1]()
Out[43]: 1

I don't understand why in the first case f[for all i in 0..9]==9

In the first case, i is a free variable. That means that Python will
get it from other place (the global namespace, likely [surely?])
>>>f=[lambda:i for i in range(10)]
f[0]()
9
>>>i=123
f[0]()
123
>>>print f[0].func_closure
None
intersting
I think I got it

[]

I have two more examples, but now I understand the difference

In [70]: x = [eval("lambda:i") for i in range(10)]
In [71]: y = [eval("lambda:%i" % i) for i in range(10)]

I think [71] is most obvious what the programmer intends

Thx
Dec 8 '06 #3
In <el**********@news2.rz.uni-karlsruhe.de>, Schüle Daniel wrote:
I have two more examples, but now I understand the difference

In [70]: x = [eval("lambda:i") for i in range(10)]
In [71]: y = [eval("lambda:%i" % i) for i in range(10)]

I think [71] is most obvious what the programmer intends
But unnecessary use of `eval()` is evil. You can spell it this way:

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

The `x` is a local name and default values are evaluated and bound when
the function is created.

Ciao,
Marc 'BlackJack' Rintsch
Dec 8 '06 #4
On 7 dic, 22:53, Schüle Daniel <u...@rz.uni-karlsruhe.dewrote:
In [38]: f = [lambda:i for i in range(10)]
In [39]: ff = map(lambda i: lambda : i, range(10))
In [40]: f[0]()
Out[40]: 9
In [41]: f[1]()
Out[41]: 9
In [42]: ff[0]()
Out[42]: 0
In [43]: ff[1]()
Out[43]: 1

I don't understand why in the first case f[for all i in 0..9]==9
In the first case, i is a free variable. That means that Python will
get it from other place (the global namespace, likely [surely?])
>>f=[lambda:i for i in range(10)]
f[0]()
9
>>i=123
f[0]()
123
>>print f[0].func_closure
None

In the second case, the inner i is a free variable, but local to its
enclosing scope (outer lambda). It's a closure:
>>ff = map(lambda i: lambda : i, range(10))
ff[4]()
4
>>ff[4].func_closure
(<cell at 0x00ACEA90: int object at 0x00995344>,)
>>i=321
ff[4]()
4
what is different from (more usefull)

In [44]: f = ["%i" % i for i in range(10)]
In [45]: f
Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
This is a simple expression evaluated at each iteration
doing it like this works again

In [54]: def d(x):
....: return lambda:x
....:
x inside lambda is a free variable, but since it's local to d, this
becomes a closure:
>>d(1)
<function <lambdaat 0x00A35EF0>
>>d(1).func_closure
(<cell at 0x00A2A4F0: int object at 0x00995338>,)
In [55]: f = [d(i) for i in range(10)]
In [56]: f[0]()
Out[56]: 0
In [57]: f[1]()
Out[57]: 1
This is similar to the ff example above
in a C programmer sence I would say there seems to be no "sequence
point" which would separate "now" from "next"
No, the problem is that C has no way to express a closure; this is a
functional concept absolutely extraneous to the C language.

--
Gabriel Genellina

Dec 8 '06 #5

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

Similar topics

5
by: George Hester | last post by:
If I have 1 Virtual Dirctory with 2 different subfolders are these 2 different folders different virtual folders? Or if I have 2 different Virtual Directories each containing a subfolder are these...
5
by: Dung Ping | last post by:
The following code produces different dates on different browers on my computer. For today on the IE it is: 2005.9.27 which is correct, but on FF is: 105.9.27 which in incorrect. I copied the...
2
by: Che | last post by:
Greetings, I am writing an application that uses an extendible XML file. in order to validate that XML I use a main XSD and in additional - few more extensions XSD's that uses the types in the...
8
by: Mary Jane Pronio | last post by:
Any information would be greatly appreciated~~!! Thanks!
1
by: barbaraJacob | last post by:
Hi everyone! Must deliver my project tomorrow....It's nearly finished. I've set up a website to be published in 3 different domains. - same webserver (I mean physically the same machine) -...
1
by: stefari | last post by:
Hi all I have to develop an embedded database using memory FLASH and memory RAM Some tables must be saved in RAM and some tables in FLASH ( RAM and FLASH are two different mounted directories in...
2
by: Stefano Farina | last post by:
Hi all I have to develop an embedded database using memory FLASH and memory RAM Some tables must be saved in RAM and some tables in FLASH ( RAM and FLASH are two different mounted directories in...
3
by: Nerrad | last post by:
hi guys, i have a project on my hand and what i'm told to do is to create a log-in feature using tables and forms. Da question is that is there any way to allow different users different access...
5
by: Christopher Brewster | last post by:
I am running the same script on the same data on two different machines (the folder is synchronised with Dropbox). I get two different results. All the script does is count words in different...
14
by: shyam.lingegowda | last post by:
Hi all, If I have two c++ programs and I compile that using two different compilers on Unix. Is it possible for these two exe's to communicate with one another. Since the way the exe's are...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.