Simple recursive sum function | what's the cause of the weird behaviour?

Peter Otten __peter__ at web.de
Sat Jul 6 15:19:30 CEST 2013


Russel Walker wrote:

> Since I've already wasted a thread I might as well...
> 
> Does this serve as an acceptable solution?
> 
> def supersum(sequence, start=0):
>     result = type(start)()
>     for item in sequence:
>         try:
>             result += supersum(item, start)
>         except:
>             result += item
>     return result

That depends on what is an acceptable result ;)
For instance:

>>> supersum([2, 3], 1)
5
>>> supersum([[1], ["abc"]], [])
[1, 'a', 'b', 'c']





More information about the Python-list mailing list