3 ms·
> But, as another practical example: "sum" in Python works for numbers, but I've seen folks assume that it works for anything that you can use + on, and so try
by vostok 9y ago
> But, as another practical example: "sum" in Python works for numbers, but I've seen folks assume that it works for anything that you can use + on, and so try and use strings with it.
I don't know python very well, but aren't string special cased? It does work with lists.
sum([[1,2,3],[4,5,6]], [])
- nine_k 9y agoIt does if you specify a sequence of list as the first argument, and an empty list as an initial value.
- vostok 9y agoThis is because the start argument defaults to 0. It doesn't make sense to add a list and 0. The same thing does not work for a sequence of strings and an empty string. sum(["abc","def"], "") Throws TypeError: sum() can't sum strings [use ''.join(seq) instead]