Thanks to visit codestin.com
Credit goes to github.com

Skip to content

functools.compose to chain functions together #44584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
catlee mannequin opened this issue Feb 15, 2007 · 9 comments
Closed

functools.compose to chain functions together #44584

catlee mannequin opened this issue Feb 15, 2007 · 9 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@catlee
Copy link
Mannequin

catlee mannequin commented Feb 15, 2007

BPO 1660179
Nosy @loewis, @birkenfeld, @rhettinger, @ncoghlan, @catlee
Files
  • functools.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/loewis'
    closed_at = <Date 2009-08-15.05:32:20.808>
    created_at = <Date 2007-02-15.00:17:07.000>
    labels = ['library']
    title = 'functools.compose to chain functions together'
    updated_at = <Date 2009-08-15.05:32:20.808>
    user = 'https://github.com/catlee'

    bugs.python.org fields:

    activity = <Date 2009-08-15.05:32:20.808>
    actor = 'rhettinger'
    assignee = 'loewis'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2007-02-15.00:17:07.000>
    creator = 'catlee'
    dependencies = []
    files = ['7762']
    hgrepos = []
    issue_num = 1660179
    keywords = ['patch']
    message_count = 9.0
    messages = ['51873', '51874', '51875', '51876', '51877', '51878', '51879', '91591', '91592']
    nosy_count = 5.0
    nosy_names = ['loewis', 'georg.brandl', 'rhettinger', 'ncoghlan', 'catlee']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1660179'
    versions = ['Python 2.6']

    @catlee
    Copy link
    Mannequin Author

    catlee mannequin commented Feb 15, 2007

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

    @catlee catlee mannequin closed this as completed Feb 15, 2007
    @catlee catlee mannequin assigned loewis Feb 15, 2007
    @catlee catlee mannequin added the stdlib Python modules in the Lib dir label Feb 15, 2007
    @catlee catlee mannequin closed this as completed Feb 15, 2007
    @catlee catlee mannequin assigned loewis Feb 15, 2007
    @catlee catlee mannequin added the stdlib Python modules in the Lib dir label Feb 15, 2007
    @birkenfeld
    Copy link
    Member

    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?

    @ncoghlan
    Copy link
    Contributor

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

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Feb 15, 2007

    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?

    @rhettinger
    Copy link
    Contributor

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

    @catlee
    Copy link
    Mannequin Author

    catlee mannequin commented Feb 15, 2007

    lambda k: k[0][1] is fine for this example.

    I just thought that since operator.itemgetter existed that there should be a way to represent a series of __getitem__ calls in a similar way...and also that a way of composing functions would be generally useful to have in the stdlib.

    FWIW, I think that the compose function would make it simpler to compose a set of functions that is determined at run-time.

    It's also my experience that newbies have a hard time with lambda's :)

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Feb 16, 2007

    I'm rejecting the patch, because YAGNI. I agree that the quality of the patch is good; thanks for submitting it. I encourage you to post it to the Python Cookbook (if it's not there already); perhaps people may come back with an actual use case some day.

    As for composing functions that are determined dynamically: this can be done already. Assume f and g are variables, you can do lambda arg:f(g(arg)). What you can't do is if you have a variably-sized list of functions to compose, but again, I think YAGNI (if that ever happens, writing a loop is easy enough).

    Wrt newbies: I agree they have difficulties with lambda, but I think that is because they always have difficulties with first-class and higher-order functions. So they likely have the same problems with itemgetter and attrgetter (which are higher-order), and would have problems with compose. Even though I believe I can deal with callable objects just fine, I had problems understanding the example, because the order of the itemgetters is reversed to the "logical" order - although it is the standard order for composition in mathematics.

    @rhettinger
    Copy link
    Contributor

    I concur with Martin's reasons for closing the patch.

    1 similar comment
    @rhettinger
    Copy link
    Contributor

    I concur with Martin's reasons for closing the patch.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants