Why is 2++++++++2 valid in python?
Is there a reason why it should not be?
I’m confused because I don’t understand how it works, I’m new in Python.
Is there a specific context in which this came up that we can help explain?
If you are new, then I’d say don’t bother.
This will most likely never come up in the life of a developer.
The reason is how the Python-interpreter reads the addition command when it come to an integer and might be based on the fact that Python turns pretty much everything in objects, with the interpreter written in C.
I’d assume this all sounds very confusing for a beginner.
But as far as I can tell, the interpreter seems bluntly to ignore the additional +. They have no syntactical meaning as the interpreter is waiting for a number. But they are also not problematic enough to throw an error.
I just had another idea and tested it:
Seems like Python considers the additional “+” as signs. You can mix in some “-” and see that it will just switch between positive/negative.
So 2 + + - + - - 2 is interpreted as 2+(+(-(+(-(-2))))) and evaluated as 0.
Confirmed with 2 + - + 2 = 0 and 2 - + - 2 = 4.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.