This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: operator.methodcaller should accept additional arguments during the call
Type: enhancement Stage:
Components: Extension Modules Versions: Python 3.6, Python 3.5
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: abacabadabacaba, josh.r, rhettinger
Priority: low Keywords:

Created on 2015-10-21 18:53 by abacabadabacaba, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg253307 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2015-10-21 18:53
Currently, operator.methodcaller behaves like this:

    def methodcaller(name, *args, **kwargs):
        def caller(obj):
            return getattr(obj, name)(*args, **kwargs)
        return caller

That is, it is possible to supply arguments when the object is created but not during the call. I think that it will be more useful if it behaved like this:

    def methodcaller(name, *args, **kwargs):
        def caller(obj, *args2, **kwargs2):
            return getattr(obj, name)(*args, *args2, **kwargs, **kwargs2)
        return caller
msg253422 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-10-25 03:53
This would be easier to evaluate propertly if you supplied some meaningful use cases.  Otherwise, it is looks like hypergeneralizing.
msg253428 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2015-10-25 16:55
There are methods that accept a single argument and behave like a binary operation or a predicate. It would be useful to be able to turn them into binary functions for use with higher-order functions like map and reduce:

    reduce(methodcaller("combine"), objs) # reduce a sequence using "combine" method
    map(methodcaller("combine"), alist, blist) # combine pairs of elements
    all(map(methodcaller("compatible_with"), alist, blist)) # check that pairs of elements are compatible

This functionality is already available for overloaded operators, because operator module provides them as functions. Some methods behave similarly to operators, so I think that a similar functionality should be available for them as well.
msg253578 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-10-28 00:52
I could see the argument for this to make methodcaller more like an unbound version of functools.partial. Partial lets you prebind some things and not others, you might want to do the same thing with methods, where you prebind the method name and some arguments, but dynamically bind the instance and some additional arguments.
msg253584 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-10-28 03:31
I would rather leave lambda or plain function definitions as the way to handle any cases not covered by itergetter, attrgettr, and methodcaller.   It feels like we're working way too hard to produce more ways to do it.   

Also, the suggested use cases with all() and map() would likely be expressed more cleanly (and readably) with a list comprehension.  The reduce() example isn't clear at all (that is part of the reason it was banished to the functools module).  I would not want to encounter any of the examples during a debugging session.

I think we should decline this feature request as being "a bridge too far" and not what the zen-of-python would encourage us to do.  I can't think of any existing code that would be improved by the availability of this extension.  Instead, we ought to focus on making the core language as expressive as possible.
msg253772 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-10-31 03:18
Closing this for the reasons mentions above.
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69640
2015-10-31 03:18:43rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg253772
2015-10-28 03:31:02rhettingersetpriority: normal -> low
assignee: rhettinger
messages: + msg253584
2015-10-28 00:52:19josh.rsetnosy: + josh.r
messages: + msg253578
2015-10-25 16:55:42abacabadabacabasetmessages: + msg253428
2015-10-25 03:53:30rhettingersetnosy: + rhettinger
messages: + msg253422
2015-10-21 18:53:39abacabadabacabacreate