Re:Static types in Python
by voidiac on Friday October 14, 2016 @06:50AM
(#12707236)
Python's implementation of sum doesn't care about types, the function looks something like:
it always starts with an integer, and since int.__add__(str) returns NotImplemented it tries str.__radd__(int) instead, which raises an AttributeError because str has no method __radd__, so no summing of strings and integers.
[ Parent
| on HN ]
Actually Python does explicitly reject strings.
>>> sum(['hello', ' world'], '')
TypeError: sum() can't sum strings [use ''.join(seq) instead]
But this is done explicitly against strings. They would _just work_ if not for this restriction, because based on usage they should work.