[Python-ideas] Thoughts on lambda expressions

Michael Selik mike at selik.org
Wed Mar 2 20:59:19 EST 2016


On Thu, Mar 3, 2016 at 12:14 AM Abe Dillon <abedillon at gmail.com> wrote:

> lambda statements tend to be most expressive when used in an expected
> context (like as the 'key' argument to the sorted function or as the first
> argument to the filter function.
>

I also use lambdas for the various key= functions (sorted, min, max). I
tend to use comprehensions instead of lambdas in filter.

Still, in the examples you wrote, I might try some alternatives...

    farthest = max(words, key=partial(edit_distance, target="hello"))
    distant_words = [w for w in words if edit_distance(w, farthest) < 5]
    shortest = min(len(word.strip()) for word in distant_words)

Typing 'lambda' is not at all my concern, in fact `(x+1 from x)` takes the
> same number of keystrokes as `lambda x:x+1`. My complaint is in readability
> which is supposed to be Python's strong suit. To most people, 'lambda' is
> pretty much a nonsense word. It might as well be 'quaple'. Would you be ok
> with writing:
>
> sorted(words, key=quaple word: edit_distance(word, "hello"))
>

The phrase ``from thing`` suggests that ``thing`` is a container, not an
argument.

While I agree that reducing jargon is a good goal, the replacement with
``from`` unfortunately introduces a different jargon rather than making it
sound like natural language. As you say, ``quaple`` is nearly as effective
as ``lambda`` except for the handful of people who already knew the jargon
from other languages. Unfortunately, both ``quaple`` and ``lambda`` are
better than your usage of ``from`` because of the cognitive dissonance.
Perhaps a keyword such as ``using`` might be better.

Besides, I'd rather write that sorted example with functools.partial:

    sorted(words, key=partial(edit_distance, target='hello'))


I'm not saying I think lambdas are great, but so far I haven't seen a
convincing example for this new usage of ``from``.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160303/b9246bdf/attachment.html>


More information about the Python-ideas mailing list