Why I hate lambdas (Re: Do any of you recommend Python as a first programming language?)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 23 12:55:24 EDT 2008


On Sun, 23 Mar 2008 09:24:35 -0700, Aahz wrote:

> The problem with lambda is that too often it results in clutter (this is
> a strictly made-up example off the top of my head for illustrative
> purposes rather than any real code, but I've seen plenty of code similar
> at various times):
> 
>     gui.create_window(origin=(123,456), background=gui.WHITE,
>         foreground=gui.BLACK, callback=lambda x: x*2)
> 
> That means I need to pause reading the create_window() arguments while I
> figure out what the lambda means -- and often the lambda is more
> complicated than that.  Moreover, because the lambda is unnamed, it's
> missing a reading cue for its purpose.


And of course this would be so much better:

def double(x): return x*2

gui.create_window(origin=(123,456), background=gui.WHITE,
    foreground=gui.BLACK, callback=double)


Not.

The source of the "clutter" (having *less* code is clutter???) and 
confusion isn't the lambda, it's the callback.



-- 
Steven






More information about the Python-list mailing list