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

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 2340
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
6
by: Xavier Décoret | last post by:
Hi, I cannot find the way to do generic lambda functions using the lambda syntax in python. I am probably missing a point. For example, the code # f = lambda : print "hello" # f()
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
15
by: Matt | last post by:
Hi There, Can anyone explain me the real advantages of (other than syntax) lambda expressions over anonymous delegates? advantage for one over the other. delegate int F(int a); F fLambda = a...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
11
by: puzzlecracker | last post by:
I am not understanding what's happening in the code below (from accelerated c#) -- I know what the outcome but not how .net does it: using System; using System.Linq; public class LambdaTest...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.