lambda (and reduce) are valuable

bonono at gmail.com bonono at gmail.com
Mon Dec 12 03:59:38 EST 2005


Paul Rubin wrote:
> "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?

Or, why would you want to refactor that ? The lambdas were used as the
quickest and the most straight forward way to have the solution back
then I believe. Refactoring is an aftermath(performance, frequent
change to the same module for feature changes etc.).
Without lambda, even the first version would force the programmer to
think more about how to factor it and it seems in this case, not
necessary and waste of precious programmer time.




More information about the Python-list mailing list