I know sum function works on an iterable of numbers for ex:
>>sum([1,2,3,4])
10
>>sum([[12,13],[2,3],[4,5],[])
[12,13,2,3,4,5] This is a flatten output since the start value here is an empty list
Why doesn't sum function works for string if the start value is an empty string or any string?
>>sum(["abc","bcd"],"") or sum(["abc","bcd"],"go")
TypeError: sum() can't sum strings [use ''.join(seq) instead]
However if I do the below it works
>>class ZeroString():
def __add__(self,other):
return other
>>print(sum(["go","there"],ZeroString()))
"gothere"
Could you please explain how does the above code works since __add__
method in ZeroString returns a string. This is similar to sum(["abc","bcd"],"") or sum(["abc","bcd"],"go")
?