lambda

Robert Cragie rcc at jennic.com
Thu Mar 30 10:20:13 EST 2000


Joey Gibson <joeyGibson at mindspring.com> wrote in message
news:9sj4esgeh2i9emc4p249ksrj04u1bdm7ur at 4ax.com...
> On Wed, 29 Mar 2000 10:19:04 GMT, morbid at doesnotlikespam.inet.net.nz
> (Morbid Curiosity) wrote:
>
> ||| On 28 Mar 2000 22:37:53 -0800, Falknor <falknor at worldspy.net> wrote:
> ||| >Hmm, what exactly is the use of lambda?  It just isn't clickin for
> ||| >some reason....  Are they just syntactical candy (such as the ternary
> ||| >operator ?: in C++ is syntactical candy..) or what. *ponders*  Anyone
> ||| >who could explain it, and gimme a little example would have my thanx.
:)
> |||
> ||| Lambda functions come from a functional programming paradigm, but I've
> ||| found a couple of situations where they're really handy in a general
> ||| imperative program, too. Have a look at this code snippet:
>
> It took me a while to 'get' lambdas at first too. They are most useful
> (IMO) when paired with map(), filter() and reject(). These functions take
a
> function as the first argument. Using a lambda allows you to declare the
> function inline as opposed to declaring the function earlier.
>
> For example, this is applying a function to a list without lambdas:
>
> aList = ['fred', 'ted', 'tom', 'bob', 'foo']
>
> def myfunc(x):
> return x * 2
>
> res = map(myfunc, aList)
>
> And here it is with lambdas:
>
> aList = ['fred', 'ted', 'tom', 'bob', 'foo']
>
> res = map(lambda x: x * 2, aList)
>
> I think you can also use them as defaults arguments to functions that
> need a function as a parameter.

Lambda's are also useful for the following. Say you want to declare a load
of Tkinter (or PMW) widgets with a common callback (in this case a PMW entry
field 'return' callback). If you just use a single 'def', then you don't
know which widget it was called back from, e.g.

    [snip]
    def doRegisterForm(self):
        for reg in registers:
            entryField = Pmw.EntryField(self.dataFrame, entry_width=8,
                                        validate='hexadecimal',
                                        value='0',
                                        command=takeAction,
                                        label_text=reg,
                                        labelpos=W, labelmargin=1)
            entryField.pack(side=TOP, expand=YES, fill=X)

    def takeAction(self):
        # Er, which widget did it come from?
    [snip]

If you use a lambda, it becomes very neat - as the parser works through the
loop, it substitutes 'reg' in the lambda expression, effectively creating a
set of 'intermediate' functions which get assigned to the callback for each
widget. On the callback, the appropriate value is passed for 'reg', so we
know which register it is.

    [snip]
    def doRegisterForm(self):
        for reg in registers:
            entryField = Pmw.EntryField(self.dataFrame, entry_width=8,
                                        validate='hexadecimal',
                                        value='0',
                                        command=lambda s=self, r=reg:
s.takeAction(r),
                                        label_text=reg,
                                        labelpos=W, labelmargin=1)
            entryField.pack(side=TOP, expand=YES, fill=X)

    def takeAction(self, reg):
        # Now I know which widget it came from
        print reg
    [snip]

I'm sure there are other ways to do it, but this is neat.

Robert Cragie





More information about the Python-list mailing list