Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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]], [])


It does if you specify a sequence of list as the first argument, and an empty list as an initial value.


This 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]




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: