My editor warns me when I compare my_var == None, but no warning when I use my_var is None.
I did a test in the Python shell and determined both are valid syntax, but my editor seems to be saying that my_var is None is preferred.
Is this the case, and if so, why?
Summary:
Use
iswhen you want to check against an object’s identity (e.g. checking to see ifvarisNone). Use==when you want to check equality (e.g. Isvarequal to3?).Explanation:
You can have custom classes where
my_var == Nonewill returnTruee.g:
ischecks for object identity. There is only 1 objectNone, so when you domy_var is None, you’re checking whether they actually are the same object (not just equivalent objects)In other words,
==is a check for equivalence (which is defined from object to object) whereasischecks for object identity: