[Python-ideas] Add additional special method lookups to math module

Paul Moore p.f.moore at gmail.com
Thu Nov 10 12:23:01 CET 2011


On 9 November 2011 23:02, Steven D'Aprano <steve at pearwood.info> wrote:
> * Instead of having to decide what operations should be supported ahead of
> time, perhaps there is a way for types to register themselves with the math
> module, e.g. say "I support sin, but not sinh". Somewhat akin to the way
> ABCs work, at least conceptually. One advantage of that may be that numeric
> classes won't have to use dunder methods for supporting the math module,
> e.g. MyNumber might register sin rather than __sin__.

I haven't checked the details, but isn't this *exactly* how ABCs work?
So math could first check for exact floats (for performance), then
check for whether the object is an instance of the
"TranscendentalMaths" ABC, and finally fall back to converting to
float.

All of this is ignoring the question of whether it's acceptable to
change the documented contract of math to only work on actual floats,
of course...

Paul.



More information about the Python-ideas mailing list