I am still very puzzled on why sum excludes a string from being used as a start. That is the ultimate of not doing one thing that’s understood by all users. It wouldn’t have broken at all if behind the curtains if a string sum would have been implemented differently. The only problem I can see that when an object is inherited from str had defined a __add__
method. But that could have been detected by checking if the __add__
of start equals the __add__
method of str. Like (pseudo code)
def sum(iterable, start=0):
if isinstance(start, str) and start.__add__ == str.__add__:
return start + "".join(iterable)
return orgsum(iterable, start) # orgsum is like current sum without a TypeError for str's.
I think it’s time to rethink this, as the developers (@guido and @rhettinger ?) seem to have overseen this possibility.