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

Fuzzyman fuzzyman at gmail.com
Sun Mar 23 15:22:44 EDT 2008


On Mar 23, 4:24 pm, a... at pythoncraft.com (Aahz) wrote:
> In article <a9idnYTzRISC63janZ2dnUVZ_jGdn... at comcast.com>,
> Jeff Schwab  <j... at schwabcenter.com> wrote:
>
>
>
> >Also, despite reassurances to the contrary, I still get the impression
> >that there is a strong anti-lambda sentiment among the Python "in"
> >crowd.  Is it just a question of the word "lambda," as opposed to
> >perceived cleaner syntax?
>
> 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.


I find lambdas invaluable very frequently - often to avoid the reading
clutter.

* Transforming function calls

    something.Event += lambda sender, event: real_handler()

* Properties

    name = property(lambda self: self.__name, __set_name)

* Very short functions where extra lines reduce readability

    if direction == 'left':
        transform = lambda x, y: -x
    else:
        transform = lambda x, y: x

* Functions that take functions as arguments

    old_list = [(3,1), (2, 2), (1, 3)]
    new_list = sorted(old_list, key=lambda x: x[1])

    new_data = process(data_Set, lambda x: x+1)
    new_data2 = process(data_Set, lambda x: x-1)


I don't think any of these are unreadable or improved by defining a
'real' function.

Michael
http://www.ironpythoninaction.com

> --
> Aahz (a... at pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> "It is easier to optimize correct code than to correct optimized code."
> --Bill Harlan




More information about the Python-list mailing list