[Python-Dev] PEP 443 - Single-dispatch generic functions

PJ Eby pje at telecommunity.com
Thu May 23 20:59:01 CEST 2013

On Thu, May 23, 2013 at 11:11 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> Is the debate between 1 and 2, or 1 and 3? Is it even possible to implement
> 3 without having 2 different names for "register"?

Yes.  You could do it as either:
    def doit(foo: int):

by checking for the first argument to register() being a function, or:

    def doit(foo: int):

by using a default None first argument.  In either case, you would
then raise a TypeError if there wasn't an annotation.

As to the ability to do multiple types registration, you could support
it only in type annotations, e.g.:

    def doit(foo: [int, float]):

without it being confused with being multiple dispatch.

One other thing about the register API that's currently unspecified in
the PEP: what does it return, exactly?

I generally lean towards returning the undecorated function, so that if you say:

    def do_int(foo: int):

You still have the option of calling it explicitly.  OTOH, some may
prefer to treat it like an overload and call it 'func' every time, in
which case register should return the generic function.  Some guidance
as to what should be the official One Obvious Way would be helpful

(Personally, I usually name my methods explicitly because in debugging
it's a fast clue as to which piece of code I should be looking at.)

More information about the Python-Dev mailing list