[Python-ideas] Fast sum() for non-numbers

Steven D'Aprano steve at pearwood.info
Wed Jul 3 19:58:31 CEST 2013


On 04/07/13 02:58, Joshua Landau wrote:

> What's wrong with sum, if sum is fast?

sum simply cannot *always* be fast. E.g. summing tuples will still be slow even with your suggestion.

Using sum() on anything other than numbers is conceptually dubious; yes, sum() is intended for addition, but it's intended for *numeric* addition. It's unfortunate that Python uses + for concatenation, we're stuck with it until Python 4000, but if you're using sum() to add lists, tuples, or other non-numbers, you're living in a state of sin. (A venal sin rather than a mortal sin.) It's allowed, but we shouldn't encourage it, and treating sum() as if it were the One Obvious Way to concatenate data is, in my strong opinion, the wrong thing to do.

In my opinion, the One Obvious Way to concatenate a lot of arbitrary data is list.extend, not sum.

I can't gather any enthusiasm for optimizing sum to speed up concatenation. I'm at best indifferent towards the specific proposal to speed up sum of lists; I'm hostile to any attempt to encourage people to treat sum() as the preferred way to concatenate large amounts of data, because that will surely lead them into bad habits and will end with them trying to sum() a lot of tuples or linked lists or something and getting O(n**2) performance.


-- 
Steven


More information about the Python-ideas mailing list