unexpected behaviour of lambda expression

Discussion in 'Python' started by leonhard.vogt@gmx.ch, Oct 9, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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>
    Fredrik Lundh, Oct 9, 2006
    #2
    1. Advertising

  3. Duncan Booth Guest

    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
    Duncan Booth, Oct 9, 2006
    #3
  4. Guest

    Fredrik Lundh schrieb:

    > 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, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    4
    Views:
    2,947
    scoude
    Jan 12, 2011
  2. Steven Van den Berghe

    unexpected map behaviour

    Steven Van den Berghe, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    417
    Christian Jan├čen
    Aug 28, 2003
  3. Roman Suzi
    Replies:
    13
    Views:
    606
    Bengt Richter
    Jan 7, 2005
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    175
    Sean O'Halpin
    Mar 30, 2009
  5. Haochen Xie
    Replies:
    4
    Views:
    242
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page