Skip to content
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

operator.methodcaller should accept additional arguments during the call #69640

Closed
abacabadabacaba mannequin opened this issue Oct 21, 2015 · 6 comments
Closed

operator.methodcaller should accept additional arguments during the call #69640

abacabadabacaba mannequin opened this issue Oct 21, 2015 · 6 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@abacabadabacaba
Copy link
Mannequin

abacabadabacaba mannequin commented Oct 21, 2015

BPO 25454
Nosy @rhettinger, @MojoVampire

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/rhettinger'
closed_at = <Date 2015-10-31.03:18:43.710>
created_at = <Date 2015-10-21.18:53:39.200>
labels = ['extension-modules', 'type-feature']
title = 'operator.methodcaller should accept additional arguments during the call'
updated_at = <Date 2015-10-31.03:18:43.685>
user = 'https://bugs.python.org/abacabadabacaba'

bugs.python.org fields:

activity = <Date 2015-10-31.03:18:43.685>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2015-10-31.03:18:43.710>
closer = 'rhettinger'
components = ['Extension Modules']
creation = <Date 2015-10-21.18:53:39.200>
creator = 'abacabadabacaba'
dependencies = []
files = []
hgrepos = []
issue_num = 25454
keywords = []
message_count = 6.0
messages = ['253307', '253422', '253428', '253578', '253584', '253772']
nosy_count = 3.0
nosy_names = ['rhettinger', 'abacabadabacaba', 'josh.r']
pr_nums = []
priority = 'low'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue25454'
versions = ['Python 3.5', 'Python 3.6']

@abacabadabacaba
Copy link
Mannequin Author

abacabadabacaba mannequin commented Oct 21, 2015

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

@abacabadabacaba abacabadabacaba mannequin added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Oct 21, 2015
@rhettinger
Copy link
Contributor

This would be easier to evaluate propertly if you supplied some meaningful use cases. Otherwise, it is looks like hypergeneralizing.

@abacabadabacaba
Copy link
Mannequin Author

abacabadabacaba mannequin commented Oct 25, 2015

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.

@MojoVampire
Copy link
Mannequin

MojoVampire mannequin commented Oct 28, 2015

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.

@rhettinger
Copy link
Contributor

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.

@rhettinger rhettinger self-assigned this Oct 28, 2015
@rhettinger
Copy link
Contributor

Closing this for the reasons mentions above.

@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
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant