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

Jeremy Sanders jeremy at jeremysanders.net
Wed Nov 9 21:06:46 CET 2011


Case Van Horsen wrote:

> 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__.

Wouldn't duct typing the functions in math and cmath be equivalent and 
wouldn't slow down normal users?

Howver, I'm not convinced that it's very nice changing the behaviour of 
built-in modules depending on the presence another module anyway, I'd just 
supply another module with the same api - people could always mess around 
with the import path if they really want to override the default.

import math

def mysin(x):
   xxxx
math.sin = mysin

Jeremy




More information about the Python-ideas mailing list