[Patches] [ python-Patches-1660179 ] functools.compose to chain functions together

SourceForge.net noreply at sourceforge.net
Thu Feb 15 21:03:32 CET 2007


Patches item #1660179, was opened at 2007-02-14 19:17
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1660179&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 2.6
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Chris AtLee (catlee)
Assigned to: Martin v. Löwis (loewis)
Summary: functools.compose to chain functions together

Initial Comment:
The motivation for this patch was to be able to chain together operator.itemgetter objects so that I could sort a list of items that have the form ((a,b), c) by the 'a' or 'b' part of the item.

Sorting by 'b' can be accomplished with:
l.sort(key=compose(itemgetter(1), itemgetter(0)))

compose(itemgetter(1), itemgetter(0)) is equivalent to

def c(v):
    return itemgetter(1)(itemgetter(0)(v))

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2007-02-15 15:03

Message:
Logged In: YES 
user_id=80475
Originator: NO

IMO, the lambda approach is more readable and more applicable to a variety
of situations.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2007-02-15 14:51

Message:
Logged In: YES 
user_id=21627
Originator: NO

I guess the same (can use a lambda) could be said about the entire
operators module, and the "entire" functools module.

I admit that in the specific example given, I would also find a lambda
easier to read (in particular because it's more obvious that it is
[0][1]).

catlee, what do you dislike about the lambda notation?

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2007-02-15 07:47

Message:
Logged In: YES 
user_id=1038590
Originator: NO

Patch quality also looks good to me.

However, my main concerns would be whether or not the use case comes up
often enough to justify inclusion in the standard library, and at what
point it makes more sense to just use a lambda expression. For example, I
think most people would find this easier to understand than the compose
based version:

    l.sort(key=(lambda k: k[0][1]))


----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2007-02-15 05:25

Message:
Logged In: YES 
user_id=849994
Originator: NO

The patch looks good, is complete (one nit, "Returns" should be "Return"
in the doc) and the functionality belongs into functools.
What do you think, Martin?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1660179&group_id=5470


More information about the Patches mailing list