sum for sequences?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Mar 29 19:19:28 EDT 2010


On Mon, 29 Mar 2010 07:40:54 -0700, Patrick Maupin wrote:

> On Mar 28, 9:45 pm, Steven D'Aprano
> <ste... at REMOVE.THIS.cybersource.com.au> wrote:
>> And what about tuples? And subclasses of list/tuples? How many
>> different types need to be optimized?
> 
> One of the beautiful things about Python is that, for most things, there
> are few surprises for even new users.  "There should be one obvious way
> to do it" for the user means that, sometimes, under the hood, there are
> a lot of special cases for the implementers.

It never ceases to amaze me how often people simply don't understand this.

"There should be one obvious way to do it" is the opposite of "NO obvious 
way", not of "many ways which may or may not be obvious". The complete 
quote from the Zen makes that clear:

There should be one-- and preferably ONLY one --obvious way to do it.
[Emphasis added]

And don't forget the next line:

Although that way may not be obvious at first unless you're Dutch.

Python is under no compulsion to make "the obvious way" obvious to anyone 
except Guido. It's a bonus if it happens to be obvious to newbies, not a 
requirement.

And besides, what is "it" you're talking about?

* Adding integers, decimals or fractions, or floats with a low
  requirement for precision and accuracy? Use sum.

* Adding floats with a high requirement for precision and accuracy? 
  Use math.fsum.

* Concatenating strings? Use ''.join.

* Joining lists? Use [].extend.

* Iterating over an arbitrary sequence of arbitrary sequences? 
  Use itertools.chain.

That's five different "its", and five obvious ways to do them.


>> 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.
> 
> Right now, it's probably not, because when somebody sums a large list
> and gets thwacked on the head by the lack of efficiency, they then come
> here and get thwacked because "everybody knows" they should user
> itertools or something else; not sum().

Exactly. If you're adding a large number of large lists, you're already 
doing it wrong. Making sum more efficient is just a band aid.


>> The primary use case for sum is adding numbers when floating point
>> accuracy is not critical. If you need float accuracy, use math.fsum.
> 
> See, I think the very existence of math.fsum() already violates "there
> should be one obvious way to do it."

How does the existence of math.fsum contradict the existence of sum?




-- 
Steven



More information about the Python-list mailing list