I really don't understand Lambda!

Ian Bicking ianb at colorstudy.com
Mon Oct 14 20:39:40 CEST 2002


On Mon, 2002-10-14 at 13:22, Bob van der Poel wrote:
> > On Thu, 2002-10-10 at 18:49, Bob van der Poel wrote:
> > > Just when I thought I was understanding lambda, it totally confuses me.
> > > I have the following statement in my program, which works:
> > >
> > >       wg.bind("<Return>", lambda s=wg: seq.setSize(wg) )
> > 
> > I think you want:
> > 
> > wg.bind("<Return>", lambda e, s=wg: seq.setSize(s))
> > 
> > The callback is always called with the event as the one argument (even
> > if you don't care about the event).
> 
> Understanding a bit more... Am I correct in understanding that the args
> (if that is what they are called) between the 'lambda' and the ':' are
> evaluated when the bind is first evaluated....  So, if 'wg' is changed
> later the 's' is in it's own namespace and not affected.

Yes, that is basically true -- if 'wg' is changed in place (like you
change an instance variable) then the lambda will still refer to that
same object, but if you assign something else to 'wg', the lambda won't
see that change of assignment.  I.e., 's' is set to the *value* of 'wg'
at the time you create the lambda.

  Ian






More information about the Python-list mailing list