I really don't understand Lambda!

Tim Roberts timr at probo.com
Sat Oct 12 02:23:47 EDT 2002


Bob van der Poel <bvdpoel at kootenay.com> wrote:
>...
>However, originally, I had tried:
>
>	wg.bind("<Return>", lambda s=wg: seq.setSize(s) )
>...
>So, if we don't need to use the variable, why not leave out the
>assignment:
>
>	wg.bind("<Return>", lambda : seq.setSize(wg) )
>
>So, (1) do we always need some kind of assignment between the 'lambda'
>and the ':'?

Because until Python 2.1, each lambda had its own local namespace.  It
could not see items in the outer namespace.  In your example, the lambda
would not have been able to see "wg".  You had to pass it as a parameter.

With the nested scopes feature in 2.1, the extra parameter is no longer
necessary, but there's a lot of code out there that wants to be backwards
compatible.
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list