[Python-Dev] Fwd: summing a bunch of numbers (or "whatevers")

Jack Diederich jack@performancedrivers.com
Sun, 20 Apr 2003 10:29:04 -0400

On Sun, Apr 20, 2003 at 03:42:26AM -0500, Ka-Ping Yee wrote:
> New question: what is add([])?  If add() is really polymorphic, then
> this should probably raise an exception (just like min() and max() do).
> That would lead to idioms such as
>     add(numberlist + [0])
>     add(stringlist + [''])
> I suppose those don't look too bad.  Nothing vastly better springs
> to mind.

For a large numberlist this is a problem, it causes a copy of the whole list.
Not to mention it looks like a perl coercion hack.  The third argument to 
reduce is there to avoid the hack.

so now we have

from newmodule import add
answer = add(numberlist, 0)

why don't we just write it as

from operator import add
answer = reduce(add, numberlist, 0)