unexpected behaviour of lambda expression

L

leonhard.vogt

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.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.'foo'

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

Fredrik Lundh

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.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>
 
D

Duncan Booth

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
 
L

leonhard.vogt

Fredrik said:
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.... return lambda x: t
....'foo'

Leonhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top