This is somehow a follow-up to this question
So first, you\'ll notice that you cannot perform a sum on a list of strings to concatenate them, python tel
FWIW, we can trick the interpreter into letting us use sum on strings by passing an appropriate custom class instance as the start arg to sum.
class Q(object):
def __init__(self, data=''):
self.data = str(data)
def __str__(self):
return self.data
def __add__(self, other):
return Q(self.data + str(other))
print(sum(['abc', 'def', 'ghi'], Q()))
output
abcdefghi
Of course, this is a rather silly thing to do. :)