sum() requires number, not simply __add__
Arnaud Delobelle
arnodel at gmail.com
Thu Feb 23 16:41:28 EST 2012
On 23 February 2012 21:23, Buck Golemon <buck at yelp.com> wrote:
> def sum(values,
> base=0):
> values =
> iter(values)
>
> try:
> result = values.next()
> except StopIteration:
> return base
>
> for value in values:
> result += value
> return result
This is definitely not backward compatible. To get something that has
a better chance of working with existing code, try this (untested):
_sentinel = object()
def sum(iterable, start=_sentinel):
if start is _sentinel:
iterable = iter(iterable)
try:
start = iterable.next()
except StopIteration:
return 0
for x in iterable:
start += x
return start
del _sentinel
--
Arnaud
More information about the Python-list
mailing list