Hacker News new | past | comments | ask | show | jobs | submit login

That's actually quite clever.



Thanks, but I got a little update: it won't work on strings.

The help says as much, in fact. Actually, what it says is, "Returns the sum of a sequence of numbers (NOT strings)," which I think is meant to imply it won't parse numbers out of strings, but in theory, sum should be able to concatenate strings as easily as it does lists, with the right start argument. Instead:

  >>> sum([['room315'], ['room2']], [])
  ['room315', 'room2']
but:

  >>> sum(['room315', 'room2'], '')
  TypeError: sum() can't sum strings [use ''.join(seq) instead]
Say, what???

I'm not sure, but I think sum is specifically watching for strings, and throwing a TypeError if it finds one. Otherwise, if it's simply using the + operator internally, as its list-handling behabior seems to imply, it should mash strings together just as happily! Oh, well. The join function is the right one for that job. It just seems a bit of wasted effort, to me.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: