Python tries to imitate the syntax of the English language to make it more understandable to humans, but in this case, the operator's priority is a bit confusing.
>>> val is not None True
In English, we ask if val value represented by the same object as None . In the above example, val=10 , so the answer is (correct) True .
However, from a logical, syntactical point of view, you broke the phrase and suggested that this:
>>> val is (not None) False
which, when enclosed in the corresponding pathoids, returns the expected result ( False ).
As @Martijn Pieters notes, is not its own operator , and it is the only operator working here.
If you want to write it as an ambiguous statement, using only those that do not contain spaces, you can write:
>>> not (val is None) True
But you cannot say the βintendedβ expression in English, or perhaps even write it in pseudo-code.
source share