[Python-ideas] Thoughts on lambda expressions

Alexander Walters tritium-list at sdamon.com
Wed Mar 2 17:24:39 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.)

     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.

On 3/2/2016 15:01, Abe Dillon wrote:
> I'm new here, but I know that lambda expression syntax has probably 
> been discussed to oblivion. I have searched previous posts without 
> finding anything exactly like my idea so here it is:
>
> |
> # instead of this
> hand =sorted(cards,key=lambdacard:card.suit)# sort some iterable of 
> cards by suit
>
> # do this
> hand =sorted(cards,key=(card.suit fromcard))# sort some iterable of 
> cards by suit
> #       |<= you can stop reading here and still have a good sense of 
> what the code does
> |
>
>
> More generally, I think a superior syntax for lambda would be:
>
> |
> (<expression>from<signature>)
> |
>
> The reasons I believe that's a superior syntax are:
>
> a) In the vast majority of use cases for lambda expressions the call 
> signature can be easily inferred (like in a key function), so moving 
> it after the expression tends to be more readable.
>
> b) It doesn't use the esoteric name, 'lambda' which causes its own 
> readability issues.
>
> c) It should be compatible with existing code:
>
> |
> try:
> 1/0
> exceptExceptionase:
> raise(ValueError()frome)# this is already a syntax error so 
> implementing the proposal won't break working code
> # of this form. I'm not aware of any other uses of the 'from' keyword 
> that would create
> # ambiguities
> |
>
> I'm not sure if this should actually be implemented, but I wanted to 
> share the idea anyway and get some feedback. In my opinion 'lambda' is 
> one of the less elegant pieces of Python syntax not because it's 
> restricted to a single expression, but because the name and syntax are 
> both detrimental to readability.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160302/cf5e2f8b/attachment-0001.html>


More information about the Python-ideas mailing list