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

Georg Brandl g.brandl at gmx.net
Wed Nov 9 21:35:23 CET 2011


Am 09.11.2011 07:17, schrieb Case Van Horsen:
> Currently the functions round(), math.ceil(), math.floor(), and
> math.trunc() all check for the existence of a special method
> (__round__, __ceil__, __floor__, and __trunc__). Would it be possible
> to enhance the math and cmath modules to check for the existence of a
> special method for (almost) functions? For example, math.sin(obj)
> would first check for obj.__sin__.
> 
> Rationale
> 
> I'm in the final stages of adding support for the MPFR
> (multiple-precision floating point) and MPC (multiple-precision
> complex) libraries to the next-generation of gmpy, currently known as
> gmpy2. If the special method checks are added to the math and cmath
> modules, then the new mpfr() and mpc() types can easily substitute for
> the existing float/complex types in code that uses the math or cmath
> module.
> 
> Thoughts?

Just a data point: numpy's ufuncs like sin() look for an attribute of
the same name ("sin") if you give them objects that aren't arrays or
Python/numpy numbers.

Georg




More information about the Python-ideas mailing list