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

Ka-Ping Yee ping@zesty.ca
Sun, 20 Apr 2003 03:42:26 -0500 (CDT)


On Sun, 20 Apr 2003, Brett Cannon wrote:
> I think part of the trouble here is the name.  The word "sum" just
> automatically causes one to think math.  This leads to thinking of
> multiplication, division, and subtraction.  But Alex's proposed function
> does more than a summation by special-casing the concatentation of
> strings.
>
> Perhaps renaming it to something like "combine()" would help do away with
> the worry of people wanting a complimentary version for multiplication
> since it does more than just sum numbers; it also combines strings in a
> very efficient manner.

Why not simply call it "add()", if it's going to be in the built-ins?
That seems like the most straightforward and accurate name.

It would have the same argument spec as min() and max(): it accepts
a single list argument, or multiple arguments to be added together.
Thus, no serious confusion with operator.add -- builtin add() would
work anywhere that operator.add works now.

>>> help(add)
add(...)
    add(sequence) -> value
    add(a, b, c, ...) -> value

    With a single sequence argument, add together all the elements.
    With two or more arguments, add together all the arguments.

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.


-- ?!ng