[Python-Dev] PEP 443 - Single-dispatch generic functions (including ABC support)

Steven D'Aprano steve at pearwood.info
Wed May 29 04:04:57 CEST 2013


On 29/05/13 07:27, PJ Eby wrote:
> On Tue, May 28, 2013 at 3:41 PM, Russell E. Owen <rowen at uw.edu> wrote:
>> Is it true that this cannot be used for instance and class methods? It
>> dispatches based on the first argument, which is "self" for instance
>> methods, whereas the second argument would almost certainly be the
>> argument one would want to use for conditional dispatch.
>
> You can use a staticmethod and then delegate to it, of course.  But it
> probably wouldn't be too difficult to allow specifying which argument
> to dispatch on, e.g.:
>
>      @singledispatch.on('someArg')
>      def my_method(self, someArg, ...):
>            ...
[...]
> So, it's just a few lines added, but of course additional doc, tests,
> etc. would have to be added as well.  (It also might be a good idea
> for there to be some error checking in wrapper() to raise an
> approriate TypeError if len(args)<=arg.)


I feel that specifying the dispatch argument in full generality is overkill, and that supporting two use-cases should be sufficient:

- dispatch on the first argument of functions;

- dispatch on the second argument of methods, skipping self/cls.

After all, is this not supposed to be *simple* generics? :-)

I'm vaguely leaning towards @singledispatch and @singledispatch.method for the colour of this bike shed.



-- 
Steven


More information about the Python-Dev mailing list