[Python-ideas] Thoughts on lambda expressions

Abe Dillon abedillon at gmail.com
Wed Mar 2 19:01:13 EST 2016


>
> My objections to this are twofold:
>
>     1) It looks a lot like a lot of other 'structures' in python.  It 
> looks very much like a generator expression or a tuple, so it isn't 
> immediately obvious to me what you are intending (if this was allowed 
> syntax of course.)
>

I rarely find myself wondering if something is a function call or a tuple 
or a generator expression or just an expression within '()' to promote 
order of operations. I don't see why this use case would all of a sudden 
make visual parsing all that more difficult, especially when there are 
often obvious context hints leading up to a lambda expression:

most_distant_word = max(words, key=(edit_distance(word, "hello") from word))


distant_words = filter((edit_distance(word, most_distant_word) < 5 from word
), words)


shortest_distant_word = min(distant_words, key=(len(word.strip()) from word
))

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.


    2) While it is nice to type that over `lambda :`, typing lambda is no 
> great burden, and that is already just sugar for a regular def statement.  
> I don't think there is much of a great need for even more terse ways of 
> writing an anonymous function, especially ones with the restrictions that 
> python currently places on them (no statements).  It just doesn't make a 
> lot of sense to change the syntax at this point.  But I think you knew that.
>

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"))

or would you rather write:

sorted(words, key=(edit_distance(word, "hello") from word))

Most Python constructs are elegantly readable:

with lock:
   mutate_shared_data()


if any(thing.is_metal for thing in pockets): alarm.sound()

You can write code that doesn't look like a mess of obscure symbols and 
esoteric words *except* for lambda expressions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160302/d4149bfe/attachment-0001.html>


More information about the Python-ideas mailing list