3 ms·
Here's how another popular scripting language handles this: >>> a, b = 123, 456 >>> a + b 579 >>> c, d = '123', '456' >>> c + d '123456
by RyanMcGreal 16y ago
Here's how another popular scripting language handles this:
>>> a, b = 123, 456
>>> a + b
579
>>> c, d = '123', '456'
>>> c + d
'123456'
>>> b + c
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
b + c
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I must confess that none of these behaviours surprised me when I first encountered them.
- cperciva 16y agoTypeError: unsupported operand type(s) for +: 'int' and 'str' What a silly error. Obviously "123" + 1 is "23", "123" + 2 is "3", "123" + 3 is "", and "123" + 456 is "ùÏÚ0óḚ̂qÛ=æPªuök6~;;\ðÚÕKµM¦OñÁ®"
- csbrooks 16y agoPython is one of my favorite languages, but every time I use + to concatenate strings I get a kind of gross feeling. I think one reason is that python encourages the use of duck typing, and as a result when you look at a piece of code you can't really be positive what kinds of values the variables hold. So if you're writing some code that works as long as the arguments passed in support "+", it's jarring thinking that it might run on strings, but surely won't do what you expect.