Re: problem with lambda / closures

Discussion in 'Python' started by Terry Reedy, Nov 30, 2009.

  1. Terry Reedy

    Terry Reedy Guest

    Benjamin Kaplan wrote:

    > I don't know if anyone considers python's incomplete implementation of
    > closures a "feature" but it's documented so it's not really a bug
    > either.


    I believe Python's implementation of closures is quite complete in 3.x.
    In what way do you consider it otherwise? One just has to use the right
    syntax. Closures in Python are created by nested function definitions.
    Lambda expressions create functions just like def statements and are not
    closures and do not create closure unless nested within another function
    definition. Thinking otherwise is the OP's mistake.

    > I believe there is a trick with default arguments to get this
    > to work, but I don't use lambdas enough to remember it.


    One can simulate closures by giving an un-nested function a default
    argument. This has nothing to do with whether the function is defined by
    a lambda expression or a def statement.

    Terry Jan Reedy
    Terry Reedy, Nov 30, 2009
    #1
    1. Advertising

  2. Terry Reedy writes:

    > definitions. Lambda expressions create functions just like def
    > statements and are not closures and do not create closure unless
    > nested within another function definition. Thinking otherwise is


    Seems quite closed in the top level environment to me:

    Python 2.3.4 (#1, Jul 16 2009, 07:03:37)
    >>> k = 'outer'
    >>> f = lambda : k
    >>> def test():

    ... k = 'inner'
    ... return f()
    ...
    >>> test()

    'outer'
    >>> k = 'thing'
    >>> test()

    'thing'
    Jussi Piitulainen, Dec 1, 2009
    #2
    1. Advertising

  3. Terry Reedy

    Terry Reedy Guest

    Jussi Piitulainen wrote:
    > Terry Reedy writes:
    >
    >> definitions. Lambda expressions create functions just like def
    >> statements and are not closures and do not create closure unless
    >> nested within another function definition. Thinking otherwise is

    >
    > Seems quite closed in the top level environment to me:
    >
    > Python 2.3.4 (#1, Jul 16 2009, 07:03:37)
    > >>> k = 'outer'
    > >>> f = lambda : k
    > >>> def test():

    > ... k = 'inner'
    > ... return f()
    > ...
    > >>> test()

    > 'outer'
    > >>> k = 'thing'
    > >>> test()

    > 'thing'


    I used closure in the same sense as the OP, who would have expected the
    last result to be 'outer', not 'thing'. Runtime lookup in the lexically
    defined global namespace is normal function behavior, not special
    closure behavior as most people mean is.

    Terry Jan Reedy
    Terry Reedy, Dec 1, 2009
    #3
    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. Roman Suzi
    Replies:
    13
    Views:
    600
    Bengt Richter
    Jan 7, 2005
  2. Louis Steinberg

    problem with lambda / closures

    Louis Steinberg, Nov 30, 2009, in forum: Python
    Replies:
    1
    Views:
    253
    Marco Mariani
    Nov 30, 2009
  3. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    173
    Sean O'Halpin
    Mar 30, 2009
  4. Aldric Giacomoni

    Closures / lambda question

    Aldric Giacomoni, Oct 28, 2009, in forum: Ruby
    Replies:
    12
    Views:
    182
    Waldemar Dick
    Oct 30, 2009
  5. Stu
    Replies:
    31
    Views:
    374
    Steve Klabnik
    Apr 27, 2011
Loading...

Share This Page