-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
functools.singledispatch interacts poorly with methods #76561
Comments
Consider the following: from functools import singledispatch
class Dispatch:
@singledispatch
def foo(self, a):
return a
@foo.register(int)
def _(self, a):
return "int"
@foo.register(str)
def _(self, a):
return "str"
cls = Dispatch()
cls.foo(3) # 3
cls.foo('hm') # 'hm' I find this quite unintuitive. Essentially, since singledispatch dispatches based solely on a functions first argument, it is useless on methods unless one wraps it and modifies how it uses the internal wrapper function. I believe this should be relatively easy to fix with adding a check of inspect.ismethod and then modifying the number of the checked argument where appropriate. I'm happy to write a patch if this change is seen as a good idea. |
I have also noticed this problem and I like the idea. It appeared some time ago on python-ideas, but no one has written a patch. |
Added Łukasz to the nosy list, as I'd like his +1 before accepting this change (I do still think it's a good idea, for the same reasons we added partialmethod). |
+1 |
This was fixed, so I think it can be closed. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: