sum for sequences?
Alf P. Steinbach
alfps at start.no
Mon Mar 29 03:34:07 EDT 2010
* Steven D'Aprano:
> 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.
Hm, I don't think it's documented (except if the reference implementation serves
as documentation) which one is currently used.
> And what about tuples? And subclasses of list/tuples? How many different
> types need to be optimized?
Point. One would need to check for availability of '+='.
> 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
> problem.
Agreed.
Cheers,
- Alf
More information about the Python-list
mailing list