468,284 Members | 1,625 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

unexpected behaviour of lambda expression

Please consider that example:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>s = 'foo'
f = lambda x: s
f(None)
'foo'
>>s = 'bar'
f(None)
'bar'
>>del(s)
f(None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
NameError: global name 's' is not defined

It seems to me, that f is referencing the name s instead of the string
object bound to it
i would expect the analogous behaviour to the following example:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>s = 'foo'
f = s
f
'foo'
>>s = 'bar'
f
'foo'

I could work around this but I am interested why there is that
difference.
Leonhard

Oct 9 '06 #1
3 2206
le***********@gmx.ch wrote:
Please consider that example:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>s = 'foo'
f = lambda x: s
f(None)
'foo'
>>>s = 'bar'
f(None)
'bar'
>>>del(s)
f(None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
NameError: global name 's' is not defined

It seems to me, that f is referencing the name s instead of the string
object bound to it
that's how lexical scoping works, of course.

if you want to bind to the object instead of the name, use explicit binding:

f = lambda x, s=s: s

</F>

Oct 9 '06 #2
le***********@gmx.ch wrote:
>>>f = lambda x: s
....
>>>f(None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
NameError: global name 's' is not defined

It seems to me, that f is referencing the name s instead of the string
object bound to it
Of course it is. Why would you expect a function to lookup its global
variables before it is called? Remember "f = lambda x: s" is just a
confusing way to write:

def f(x):
return s
Oct 9 '06 #3
Fredrik Lundh schrieb:
le***********@gmx.ch wrote:
Please consider that example:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>s = 'foo'
f = lambda x: s
f(None)
'foo'
>>s = 'bar'
f(None)
'bar'
>>del(s)
f(None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in <lambda>
NameError: global name 's' is not defined

It seems to me, that f is referencing the name s instead of the string
object bound to it

that's how lexical scoping works, of course.

if you want to bind to the object instead of the name, use explicit binding:

f = lambda x, s=s: s

</F>
Thank you, together with the response of Duncan it is clear to me now.
I will use something like
>>def makefunc(t):
.... return lambda x: t
....
>>s = 'foo'
f = makefunc(s)
f(None)
'foo'
>>s = 'bar'
f(None)
'foo'

Leonhard

Oct 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ben | last post: by
53 posts views Thread by Oliver Fromme | last post: by
6 posts views Thread by Xavier Décoret | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
5 posts views Thread by Octal | last post: by
21 posts views Thread by globalrev | last post: by
11 posts views Thread by puzzlecracker | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.