[Python-ideas] Including elementary mathematical functions in the python data model

Masklinn masklinn at masklinn.net
Sun Sep 26 14:33:14 CEST 2010


On 2010-09-26, at 14:07 , Nick Coghlan wrote:
> On Sun, Sep 26, 2010 at 9:05 PM, Mark Dickinson <dickinsm at gmail.com> wrote:
>> A less invasive proposal would be just to introduce __sin__, etc.
>> magic methods and have math.sin delegate to <type>.__sin__;  i.e.,
>> have math.sin work in exactly the same way that math.floor and
>> math.ceil currently work.  That would be quite nice for e.g., the
>> decimal module:  you'd be able to write something like:
>> 
>> from math import sqrt
>> root = (-b + sqrt(b*b - 4*a*c)) / (2*a)
>> 
>> to compute the root of a quadratic equation, and it would work
>> regardless of whether a, b, c were Decimal instances or floats.
>> 
>> I'm not sure how I feel about the entailed magic method explosion, though.
> 
> Couple that with the extra function call overhead (since these
> wouldn't have real typeslots) and it still seems like a less than
> stellar idea.
> 
> As another use case for solid, efficient generic function support
> though... great idea :)
> 
> Cheers,
> Nick.

Couldn't that also be managed via ABCs for numerical types? Make sqrt & al methods of those types, and roll out in the sunset, no? The existing `math` functions could check on the presence of those methods (or the input types being instances of the ABCs they need), and fall back on the current implementations if they don't match.


More information about the Python-ideas mailing list