Why does Python treat consecutive repetitions of the addition operator as just a single repetition?

Viewed 65

Today I noticed that the Python interpreter treats consecutive repetitions of the addition operator as just one repetition of the operator. For example:

>>> 1 ++ 2
3

Of what use was it to not simply raise an error when such an event occurs? I find it much more believable that a programmer who typed

>>> 1 -+-++ 2

was just out of their mind; it would be highly unlikely that this ever intentionally appears in code.

It seems to serve no purpose, for writing something like

>>> +-1

simply returns -1, demonstrating that the operation does not make the number positive, rather, it simply performs the identity operation.

2 Answers

It's mathematical. While it doesn't make sense to a normal person, to python +(+1) and -(-1) is +, +(-1) and -(+1) is -. Jumping back into the question, it isn't just python that does this. It's mathematical thinking. It's like telling a computer a variable called string is equal to 29. It sounds wrong but the computer thinks it is normal.

Related