[Python-ideas] Yet another sum function (fractions.sum)
Tal Einat
taleinat at gmail.com
Wed Aug 21 10:20:33 CEST 2013
On Wed, Aug 21, 2013 at 9:25 AM, Peter Otten <__peter__ at web.de> wrote:
> At the time I only thought about sum(), but yes, for every operation that
> has one "best" implementation per class there should be a uniform way to
> make these implementations available.
>
> Oscar Benjamin wrote:
>> One way to unify these things is with a load of new dunder
>> methods so that each type can implement its own response to the
>> standard function. Another is to have special modules that deal with
>> different things and e.g. a function for summing Rationals and another
>> for summing inexact types and so on.
>>
>> Another possibility is that you have decimal functions in the decimal
>> module and fraction functions in the fractions module and so on and
>> don't use any duck-typing (except in coercion). That may seem strange
>> for Python but it's worth remembering that most of the best numerical
>> code that has ever been written was written in languages that
>> *require* you to write separate code for each numeric type and don't
>> give any syntactic support for working with non-native types.
>
> Classmethods would be yet another option
>
> float.sum(numbers)
>
> or
>
> complex.sqrt(a)
>
> don't look bad, though I'm not sure what the right approach for int.sqrt()
> would be...
I must say, this sounds like a classic case for a cookbook recipe,
especially considering that the implementations are simple and elegant
Python code.
- Tal
More information about the Python-ideas
mailing list