[Python-ideas] Thoughts on lambda expressions

Alexander Walters tritium-list at sdamon.com
Wed Mar 2 17:38:57 EST 2016


I hit send before my thought was really complete.

I don't know if that is a contrived example or not, but python also 
already provides a more efficient and readable way of pulling attributes 
off of an object (a very common misuse of lambda/anonymous functions). 
|operator.||attrgetter, and it's cousin operator.itemgetter for item 
access.  So for your example code, there is already a cleaner way.  I 
don't know if that is your main use case.  If it is not, then perhaps a 
better example could be provided where this would prove superior?
|
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/204c458b/attachment.html>


More information about the Python-ideas mailing list