Why should you avoid using "is" and "is not"?
I just started with Python, and my online professor recommended using is
and is not
only when comparing the value with True
, False
or None
(or at least that's how I understood what he was saying.)
Now in my mind I equate is
to JavaScript ===
, and is not
with JavaScript "!=="
, and I believe that as a best practice is to use ===
and !==
how often you can avoid forced conversion types.
Firstly, if we want to use is not true is
, and is not
as much as ===
, and !==
in JavaScript, and if so, why?
source to share
Python is
and operators ==
do two different things. You cannot compare is
to JS either ===
.
Simply, it ==
checks if two identifiers are equal, but is
checks if two identifiers have the same memory address, therefore "each other"; how they behave sounds the same:
#0 equals None, so returns True
0 == None
#But it isn't None, hence returns False
0 is None
#However, 0 is 0, so returns True
0 is 0
source to share
None
is singleton, so you can compare the identity ( is
/ is not
) object instead of equality ( ==
/ !=
) - whenever you access None
, you get the same object.
For almost all other cases, you should use equality (see, for example, Why does string comparison in Python using "==" or "is" sometimes lead to different results? ). In terms of "avoid forced type conversion" - Python is strongly typed and will never convert implicitly. Also, you cannot "distinguish" a type in Python - conversion, for example. a int
to str
creates a new separate object.
This is especially important as it is None
often used to avoid flagging a return β if False
either 0
or []
or anything else that evaluates False
-y, it is a valid return evaluating "truth" that will give false results.
def yes_or_no(s):
"""Convert 'yes' or 'no' to boolean, or implicitly return None."""
if s.lower() == "yes":
return True
elif s.lower() == "no":
return False
result = some_func("foo")
if result: # wrong, covers None and False
...
if result is not None: # right, covers None only
...
Per style guide :
Comparison with singlons of a type
None
should always be done usingis
oris not
, never-executed equality operators.Also, beware of writing
if x
if you really mean ifx is not None
- eg. when checking if a variable or an argument was, the default for isNone
set to some other value. Another value could be of a type (eg container), which could be false in a boolean context!
Note that you do n't usually compare True
or False
. Again, from the style guide:
Do not compare boolean values ββwith
True
orFalse
with==
.
Yes: if greeting: No: if greeting == True: Worse: if greeting is True:
See also: Using True, False and None as return values ββin python functions
source to share
We shouldn't use is
and is not
to compare values ββother than True
, False
and None
probably because of the following strange results:
>>> 20 is 19+1
True
>>> 19+1 is 20
True
>>> 1999+1 is 2000
False
>>> -0 is 0
True
>>> -0.0 is 0.0
False
>>> -0.0 is not 0.0
True
>>> 1999+1 is not 2000
True
>>> -0.0 == 0.0
True
>>> 1999+1 == 2000
True
>>> 1999+1 != 2000
False
>>> -0.0 != 0.0
False
source to share