Discussion:
[Cython] python-ideas discussion on "multiple dispatch", also operator overloading
Stefan Behnel
2014-08-16 06:40:04 UTC
Permalink
Hi,

there's a discussion going on on python-ideas about generic functions and
multiple dispatch. Might be interesting for the fused functions
implementation in Cython.

http://thread.gmane.org/gmane.comp.python.ideas/28790/focus=28848

Cython can definitely improve its own dispatch implementation.

The reply I linked to is by Nick Coghlan, who also makes an interesting
comment on operator overloading methods and NotImplemented:

"""
A *lot* of folks make the mistake
of raising TypeError or NotImplementedError directly in their operator
overload implementations, rather than returning the NotImplemented
singleton that tells the interpreter to try the other type. Even some
of the CPython builtins get that wrong, since the sq_concat and
sq_repeat slots in C don't properly support the type coercion dance,
so you *have* to raise the exception yourself if you're only
implementing those without implementing nb_add and nb_mul (types
defined in Python automatically populate both sets of C level slots if
you define __add__ or __mul__).
"""

I think Cython could help its users here to a certain extent, if only by
warning about raising TypeError explicitly in these methods and encouraging
better behaviour.

Stefan

Loading...