Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unexpected behaviour of lambda expression

Reply
Thread Tools

unexpected behaviour of lambda expression

 
 
leonhard.vogt@gmx.ch
Guest
Posts: n/a
 
      10-09-2006
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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>



 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      10-09-2006
(E-Mail Removed) 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
 
Reply With Quote
 
leonhard.vogt@gmx.ch
Guest
Posts: n/a
 
      10-09-2006
Fredrik Lundh schrieb:

> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM



Advertisments