Python ( or general programming ). Why use <> instead of != and are there risks?
I think if I understand correctly, a <> b
is the exact same thing functionally as a != b
, and in Python not a == b
, but is there reason to use <>
over the other versions? I kno开发者_JS百科w a common mistake for Python newcomers is to think that not a is b
is the same as a != b
or not a == b
.
- Do similar misconceptions occur with
<>
, or is it exactly the same functionally? - Does it cost more in memory, processor, etc.
<>
in Python 2 is an exact synonym for !=
-- no reason to use it, no disadvantages either except the gratuitous heterogeneity (a style issue). It's been long discouraged, and has now been removed in Python 3.
Just a pedantic note: the <>
operator is in some sense misnamed (misdenoted?). a <> b
might naturally be interpreted as meaning a < b or a > b
(evaluating a
and b
only once, of course), but since not all orderings are total orderings, this doesn't match the actual semantics. For example, 2.0 != float('nan')
is true, but 2.0 < float('nan') or 2.0 > float('nan')
is false.
The !=
operator isn't subject to such possible misinterpretation.
For an interesting take (with poetry!) on the decision to drop <>
for Python 3.x, see Requiem for an operator.
you shouldn't use <>
in python.
精彩评论