2009/12/5 George Sakkis <george.sakkis@gmail.com>:
On Sat, Dec 5, 2009 at 6:45 PM, Andre Engels <andreengels@gmail.com> wrote:
On Sat, Dec 5, 2009 at 12:55 PM, Ram Rachum <cool-rr@cool-rr.com> wrote:
I noticed that `sum` tries to add zero to your iterable. Why? Why not just skip adding any start value if none is specified?
This current behavior is preventing me from using `sum` to add up a bunch of non- number objects.
In your proposed implementation, sum([]) would be undefined.
Which would make it consistent with min/max.
And in that case the special string handling could also be dropped?
sum(["a","b"], "start") Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> sum(["a","b"], "start") TypeError: sum() can't sum strings [use ''.join(seq) instead]
This behaviour is quite bothersome. Sum can handle arbitrary objects in theory (as long as they define the correct special methods, etc.), but it gratuitously raises an exception on strings. This behaviour is also inconsistent with the following:
sum(["a","b"]) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> sum(["a","b"]) TypeError: unsupported operand type(s) for +: 'int' and 'str'
Where sum actually tries to add "a" to the default value of 0.