lambda (and reduce) are valuable

Paul Rubin http
Mon Dec 12 03:48:33 EST 2005


"Fredrik Lundh" <fredrik at pythonware.com> writes:
> a temporary factory function should be sufficient:
> 
>     def digit(label, x, y):
>         def callback():
>             # print "BUTTON PRESS", label # debug!
>             user_pressed(int(label))
>         Button(label=label, command=callback).grid(column=x, row=y)

Looking at the calculator program I wrote a while back (one of my
first Python programs, written just to play with tkinter), I see that
I actually did something like that for the buttons.  However, it also
contained (in the other order):

    unops = {'sqrt': math.sqrt,
             'sin': math.sin,
             'cos': math.cos,
             'tan': math.tan,
             'ln': math.log,
             'log': lambda x: math.log(x)/math.log(10),
             'clr x': lambda x: 0
             }

    binops = {'+': (lambda x,y: x+y),
              '-': (lambda x,y: x-y),
              '*': (lambda x,y: x*y),
              '/': (lambda x,y: x/y),
              '**': (lambda x,y: x**y)
              }

How would you refactor that, with no lambda?



More information about the Python-list mailing list