On 18 June 2017 at 07:27, Mital Ashok via Python-ideas
<python-ideas@python.org> wrote:
> Right now, an example for single dispatch would be:
>
> from functools import singledispatch
>
> @singledispatch
> def fun(arg, verbose=True):
>     if verbose:
>         print("Let me just say,", end=" ")
>     print(arg)
>
> @fun.register(int)
> def _(arg, verbose=True):
>     if verbose:
>         print("Strength in numbers, eh?", end=" ")
>     print(arg)
>
> @fun.register(list)
> def _(arg, verbose=True):
>     if verbose:
>         print("Enumerate this:")
>     for i, elem in enumerate(arg):
>         print(i, elem)
>
> But this makes a useless _ function, that should either be deleted or
> ignored.

Don't do that, give the overloads meaningful names that your test
suite can then use to check that they do the right thing independently
of the dispatch process.

Even if you're not doing unit testing at that level, the names will
also show up in exception tracebacks and other forms of introspection
(e.g. process state dumps), and seeing descriptive names like
"_fun_for_int" and "_fun_for_list" is *significantly* more informative
than seeing multiple distinct functions all called "_" or "__".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to