By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

unexpected behaviour of lambda expression

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.