sum for sequences?
steven at REMOVE.THIS.cybersource.com.au
Mon Mar 29 04:45:32 CEST 2010
On Sun, 28 Mar 2010 18:56:26 +0200, Alf P. Steinbach wrote:
> From a more practical point of view, the sum efficiency could be
> improved by doing the first addition using '+' and the rest using '+=',
> without changing the behavior.
But that would change the behaviour. The __iadd__ method is not the same
as the __add__ method, and you can't guarantee that they will behave the
same -- or even similarly.
And what about tuples? And subclasses of list/tuples? How many different
types need to be optimized?
In practical terms, does anyone actually ever use sum on more than a
handful of lists? I don't believe this is more than a hypothetical
The primary use case for sum is adding numbers when floating point
accuracy is not critical. If you need float accuracy, use math.fsum. If
you need to add more than a handful of small lists, don't use sum: just
calling extend in a loop is probably fast enough, or use itertools.chain.
Trying to turn sum into an all-singing all-dancing function optimised for
an ever-increasing number of objects is a fool's errand. The
implementation of sum in C is already complex enough: 95 lines, excluding
comments, blanks and braces, for something which is a lightweight
function with very simple semantics.
But if anyone wants to submit a patch to the bug tracker, go right ahead.
Without a patch though, I'd say that Python-Dev will consider this a non-
More information about the Python-list