lambda

Roman Suzi rnd at onego.ru
Wed May 23 16:18:02 EDT 2001


On Wed, 23 May 2001, 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()

lambda is cool!

Why not to use it in map and drop for-loop also?

map(lambda (txt, cmd), column, root=root:
      Tkinter.Button(root, text = txt, command = cmd).
        grid(row=1, col=column, sticky = 'w'),
   buttonList, range(len(buttonList)))

(I have not checked if this works, but looks fine)

>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)
>
>Laura Creighton
>
>

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Wednesday, May 23, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Never mind the facts - I know what I know." _/





More information about the Python-list mailing list