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

Case Van Horsen casevh at gmail.com
Wed Nov 9 22:42:26 CET 2011


On Wed, Nov 9, 2011 at 11:43 AM, Jim Jewett <jimjjewett at gmail.com> wrote:
> I like the idea, but there is a question of namespace size,
> particularly if there may be other meanings for some of the names.
>
> I would therefore prefer __math_sin__, __math_arctan__, etc.  (Does it
> need to be even more specific than math?)
>
> -jJ
>
Good idea. Creating a naming convention for functions that return real
or complex values would be needed. math.sin(obj) would look for
obj.__math_sin__. cmath.sin(obj) would look for obj.__cmath_sin__.

gmpy2 supports integer, rational, real, and complex numbers. The
integer, rational, and real objects would need both the __math_XXX__
and __cmath_XXX__ methods. The complex object would only support the
__cmath_XXX__ methods.

There is ambiguity with functions that take more than one argument:
atan2, copysign, fmod, hypot, and pow. If only one of the arguments
supports the special method, then that special method should be used.
If both of the arguments supports the special method AND both
arguments are of the same type, then the special method should be
used.

But what if both arguments are different types? I think it should try
the special method associated with the first argument and if that
return NotImplemented, then it should try the special method
associated with the second argument.

If the special methods ever return NotImplemented, then the normal
math/cmath handling should make a final attempt.

casevh



More information about the Python-ideas mailing list