[Python-ideas] Yet another sum function (fractions.sum)

Stefan Behnel stefan_ml at behnel.de
Mon Aug 19 18:38:23 CEST 2013


Clay Sweetser, 19.08.2013 15:47:
> On Aug 19, 2013 6:09 AM, "Peter Otten" wrote:
>>
>> If that takes on, and the number of sum implementations grows, maybe there
>> should be a __sum__() special (class) method, and the sum built-in be
>> changed roughly to
>>
>> def sum(items, start=0):
>>     try:
>>         specialized_sum = start.__sum__
>>     except AttributeError:
>>         return ... # current behaviour
>>     return specialized_sum(items, start)
>>
>> sum(items, 0.0) would then automatically profit from the clever
>> optimizations of math.fsum() etc.
> 
> Another possibility (and I'm not suggesting that it's better than the
> proposed solution above) is to make a module just for the numerous sum
> implementations people need.

-1

For summing up fractions, the fractions module is The One Obvious Place to
look. For Decimal arithmetic, my first look would always go to the decimal
module. Given that the math module deals with floating point arithmetic,
it's The One Obvious Place to look for summing up floats.

I agree with Paul Moore that the rest can be handled by documentation.

Stefan




More information about the Python-ideas mailing list