Simple recursive sum function | what's the cause of the weird behaviour?
joshua.landau.ws at gmail.com
Sat Jul 6 20:43:42 CEST 2013
On 6 July 2013 13:59, Russel Walker <russ.pobox at gmail.com> 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:
> result += supersum(item, start)
> result += item
> return result
It's probably more robust to do:
def supersum(sequence, start=0):
for item in sequence:
result = result + supersum(item, start)
result = result + item
as that way you aren't assuming the signature of type(start).
More information about the Python-list