checking if two things do not equal None
Marko Rauhamaa
marko at pacujo.net
Sun Mar 30 09:37:45 CEST 2014
Gregory Ewing <greg.ewing at canterbury.ac.nz>:
> a != b != c
>
> does *not* imply that a != c. At least it doesn't in Python; I've
> never seen any mathematicians write that, so I don't know what they
> would make of it.
Any resemblance between mathematics notation and Python is purely
coincidental. I must admit I had missed Python's chained comparisons
until this discussion, but now I looked up the definition:
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="
| "is" ["not"] | ["not"] "in"
[...]
Formally, if a, b, c, ..., y, z are expressions and op1, op2, ...,
opN are comparison operators, then a op1 b op2 c ... y opN z is
equivalent to a op1 b and b op2 c and ... y opN z, except that each
expression is evaluated at most once.
That means, in my opinion, that you should feel free to use chaining any
way you see fit. Also, the rule is crystal-clear and easy to grasp:
there's an implicit "and" there.
It's another thing, then, if it was a good idea to include chaining
there in the first place, but I trust the idea was properly vetted and
double checked against possible parsing ambiguities.
Even without chaining "is not" is a bit suspect:
>>> False is not 0
True
>>> False is (not 0)
False
>>> False is not not not 0
File "<stdin>", line 1
False is not not not 0
^
SyntaxError: invalid syntax
Marko
More information about the Python-list
mailing list