3 ms·
Sum and join mean different things. If sum didn't fail on chars I'd expect it instead to sum their binary values, which is a thing one occasionally actually doe
by karatestomp 6y ago
Sum and join mean different things. If sum didn't fail on chars I'd expect it instead to sum their binary values, which is a thing one occasionally actually does want to do, not to join/concatenate them. If it can't do that (or you have strings, as in this case, for which it makes less sense—the most sensible thing there would probably be to treat it as a byte array and do something with that, though any conceivable use of that probably breaks down with UTFs 8 or 16 instead of ascii) I'd want it to fail.
- tom_mellior 6y agoSum is the iteration of +. + on strings is concatenation. If you expected otherwise from sum, it's your expectations that might be wrong. What even is a Python string's "binary value"? Also, Python has no chars.
- uryga 6y agoso you'd expect this behavior? sum("abc") = sum(ord(c) for c in "abc") = sum([97, 98, 99]) = 294 because wow, i... can't think of why you'd ever actually want that. (though it'd be perfectly natural for byte arrays)
- stouset 6y agoByte arrays sure. Not strings. This is why having clear types is important! Strings aren't just byte arrays. Characters aren't just bytes.