lambda

Grant Edwards grante at visi.com
Wed May 23 13:12:45 EDT 2001


In article <mailman.990635563.6483.python-list at python.org>, Laura Creighton wrote:
>I use lambda for things like this:
>
>if __name__ == '__main__':
>    root = Tkinter.Tk()
>    w = Tkinter.Button(root, text='This is a very green button', width= 30, fg='green')
>    
>    buttonList = (
>        ['show', lambda x = w: x.grid(row=0, col=0, columnspan= 5)],
>        ['pink', lambda x = w: x.configure(fg='pink')],
>        ['yellow', lambda x = w: x.configure(bg='yellow')],
>        ['Exit', root.destroy],
>        )
>    column = 0
>    for txt, cmd in buttonList:
>        button = Tkinter.Button(root, text = txt, command = cmd)
>        button.grid(row=1, col=column, sticky = 'w')
>        column = column + 1
>    root.mainloop()
>
>If I had just used w.grid up there I would get a yellow button with pink
>text.   This form is concise.  What do the lambda dislikers suggest I do
>instead?  (This is a serious question)

I use it quite a bit in similar situations, e.g. when using
nested loops to create a matrix of buttons, each of which has a
command function that must know the column/row number.

-- 
Grant Edwards                   grante             Yow!  Now that I have my
                                  at               "APPLE," I comprehend COST
                               visi.com            ACCOUNTING!!



More information about the Python-list mailing list