[Python-ideas] Why does `sum` use a default for the `start` parameter?

Adam Olsen rhamph at gmail.com
Sun Dec 6 07:29:05 CET 2009

On Sat, Dec 5, 2009 at 12:19, Vitor Bosshard <algorias at gmail.com> wrote:
> I think you misunderstood my point. Sorry if I wasn't clear enough in
> my original message. I understand the performance characteristics of
> repeated concatenation vs str.join. I just wonder why the language
> goes out of its way to catch this particular occurrence of bad code,
> given there are plenty of ways to misuse sum or any other builtin for
> that matter. A newbie is more likely to get n**2 performance by using
> a for loop than sum:
> final = ""
> for s in strings:
>    final += s
> Should python refuse to compile the above snippet? The answer is an
> emphatic "no".

All the individual operations there are fine.  It's the composition
that's wrong.  Adding a sanity check would require recognizing that
pattern, and changing the semantics of an individual operation based
on what surrounds it.  Not a nice thing to do.

sum() is already a single operation (regardless of how it's
implemented), so it doesn't have that problem.

Adam Olsen, aka Rhamphoryncus

More information about the Python-ideas mailing list