Author ethan smith
Recipients ethan smith
Date 2017-12-19.22:23:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1513722188.44.0.213398074469.issue32380@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-12-19 22:23:08ethan smithsetrecipients: + ethan smith
2017-12-19 22:23:08ethan smithsetmessageid: <1513722188.44.0.213398074469.issue32380@psf.upfronthosting.co.za>
2017-12-19 22:23:08ethan smithlinkissue32380 messages
2017-12-19 22:23:08ethan smithcreate