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 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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Mary Jane Pronio |
last post by:
Any information would be greatly appreciated~~!!
Thanks!
|
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)
-...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |