python 2.3's lambda behaves old fashioned

Discussion in 'Python' started by Uwe Schmitt, Apr 29, 2004.

  1. Uwe Schmitt

    Uwe Schmitt Guest

    Hi,

    I just tried (Python 2.3)

    li = [ lambda x: x*a for a in range(10) ]

    which results in

    li[0](1) = 9
    ...
    li[9](1) = 9

    In order to achieve the intended result I had to fall back on the
    following trick:

    li = [ lambda x,a=a: x*a for a in range(10)]

    which leads to the expected result.

    Any explanations ???

    Greetings, Uwe.


    --
    Dr. rer. nat. Uwe Schmitt http://www.procoders.net
    "A service to open source is a service to mankind."
     
    Uwe Schmitt, Apr 29, 2004
    #1
    1. Advertising

  2. Uwe Schmitt

    Yermat Guest

    Uwe Schmitt wrote:
    > Hi,
    >
    > I just tried (Python 2.3)
    >
    > li = [ lambda x: x*a for a in range(10) ]
    >
    > which results in
    >
    > li[0](1) = 9
    > ...
    > li[9](1) = 9
    >
    > In order to achieve the intended result I had to fall back on the
    > following trick:
    >
    > li = [ lambda x,a=a: x*a for a in range(10)]
    >
    > which leads to the expected result.
    >
    > Any explanations ???
    >
    > Greetings, Uwe.
    >
    >


    exactly like this thread :
    http://groups.google.fr/groups?hl=f...m=tyfn0696x8w.fsf%40pcepsft001.cern.ch&rnum=2
    (search "lambda default group:comp.lang.python" on google, thread of the
    6 jan 2004)

    or at :
    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=4bca1bec20119375&rnum=6

    hint: this is a matter of scope...

    --
    Yermat
     
    Yermat, Apr 29, 2004
    #2
    1. Advertising

  3. Uwe Schmitt <> wrote in message news:<c6r3qo$1574k$-saarland.de>...
    > Hi,
    >
    > I just tried (Python 2.3)
    >
    > li = [ lambda x: x*a for a in range(10) ]
    >
    > which results in
    >
    > li[0](1) = 9
    > ...
    > li[9](1) = 9
    >
    > In order to achieve the intended result I had to fall back on the
    > following trick:
    >
    > li = [ lambda x,a=a: x*a for a in range(10)]
    >
    > which leads to the expected result.
    >
    > Any explanations ???
    >
    > Greetings, Uwe.


    This should be in the FAQ. Here is a recent thread on the subject:

    http://groups.google.it/groups?hl=i...o+scope+rules&meta=group%3Dcomp.lang.python.*

    Michele Simionato
     
    Michele Simionato, Apr 29, 2004
    #3
  4. Uwe Schmitt

    Terry Reedy Guest

    "Uwe Schmitt" <> wrote in message
    news:c6r3qo$1574k$-saarland.de...
    > I just tried (Python 2.3)


    Which did not change anything in this regard...

    > li = [ lambda x: x*a for a in range(10) ]


    which, except for function __name__ and func_name attributes, translates
    (unabbreviates) to

    li = []
    for a in range(10):
    def tem(x): return x*a
    li.append(tem)
    del tem

    which is equivalent to

    li = []
    for a in range(10):
    def tem(x): return x*__multiplier
    li.append(tem)
    __multiplier = a
    del a, tem

    > which results in
    >
    > li[0](1) = 9
    > ...
    > li[9](1) = 9


    As one would expect from second translation above if not the first.

    Its funny how some people expect default parameter objects to be
    re-calculated on every call instead of just once, while others sometimes
    expect global vars to be evaluated just once instead of on every call ;-)

    The question of whether free vars within lambdas within list comps should
    get 'early binding' instead of the usual late binding was discussed on
    Py-Dev list last fall and some this winter. I believe Guido recently said
    no change.

    Terry J. Reedy
     
    Terry Reedy, Apr 29, 2004
    #4
  5. Uwe Schmitt wrote:

    > I just tried (Python 2.3)
    >
    > li = [ lambda x: x*a for a in range(10) ]
    >
    > which results in
    >
    > li[0](1) = 9
    > ...
    > li[9](1) = 9
    >
    > In order to achieve the intended result I had to fall back on the
    > following trick:
    >
    > li = [ lambda x,a=a: x*a for a in range(10)]
    >
    > which leads to the expected result.
    >
    > Any explanations ???


    repeat after me:

    free variables bind to names, default arguments bind to objects.
    free variables bind to names, default arguments bind to objects.
    free variables bind to names, default arguments bind to objects.
    (etc)

    </F>
     
    Fredrik Lundh, Apr 29, 2004
    #5
    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:
    629
    Bengt Richter
    Jan 7, 2005
  2. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    202
    Sean O'Halpin
    Mar 30, 2009
  3. Replies:
    1
    Views:
    122
    Erwin Moller
    Dec 14, 2006
  4. Haochen Xie
    Replies:
    4
    Views:
    262
    Haochen Xie
    Mar 17, 2013
  5. Tuxedo

    The old-fashioned guestbook

    Tuxedo, Sep 16, 2013, in forum: Perl Misc
    Replies:
    4
    Views:
    1,342
    Tuxedo
    Sep 18, 2013
Loading...

Share This Page