[Python-ideas] Thoughts on lambda expressions

M.-A. Lemburg mal at egenix.com
Wed Mar 2 18:07:06 EST 2016


On 02.03.2016 21: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=lambda card: card.suit)  # sort some iterable of 
> cards by suit
> 
> # do this
> hand = sorted(cards, key=(card.suit from card))  # sort some iterable of 
> cards by suit
> #                                   |<= you can stop reading here and still 
> have a good sense of what the code does  

Why not this ?

import operator
hand = sorted(cards, key=operator.attrgetter('suit'))

# For the curious:
# https://docs.python.org/3.5/library/operator.html#operator.attrgetter

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Mar 03 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________
2016-02-19: Released eGenix PyRun 2.1.2 ...       http://egenix.com/go88

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list