Introduction
The Python is
operator determines whether two references (variables or literals) point to the same object in memory, i.e. whether they are the same object. The is
operator is different from the equality operator ==
, which checks whether the values of potentially two objects are equal. For example 0 == 0.0
is True
, even though they are of different types (int
and float
). However, 0 is 0.0
is False
because they are not the same object.
Using the is Operator
The is
operator checks whether two references point to the same object. If they do, is
returns True
, otherwise, it returns False
.
Here is the basic syntax for the is
operator:
obj1 is obj2
Example:
a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) # True print(a is c) # False
Output:
True False
In the example above, a is b
is True
because b
is assigned to the same list object as a
. On the other hand, a is c
is False
because c
is a different list object with the same content.
Checking Singleton Instances
Singletons are objects that are instantiated only once. The most common example in Python is None
, which is a Python object used for representing null or “nothing”. The Boolean values True
and False
are also singletons.
The is
operator (and not ==
) is considered best to test for equality with a singleton, such as None
, because of following reasons:
- Correctness: The
is
operator checks for object identity, meaning it checks whether two references point to the same object. Usingis
guarantees that the object you are comparing holds the singleton value. The==
operator only compares values, and its behavior can change depending on the types of objects involved, e.g.0 == 0.0
. Additionally, the behavior of==
can be overridden by defining the__eq__
method in a class, and theis
operator cannot be overridden. - Performance: The
is
operator is typically faster than==
because it only compares object identities (i.e., memory addresses) rather than potentially invoking an equality method. - Readability and Convention: Using
is
to check for singletons, especiallyNone
, is a widely accepted convention in Python code. It makes the intention of the check clearer to other programmers who read your code.
Example:
x = None print(x is None)
Output:
True
In the example above, x
and the literal None
both point to the same None
object, so x is None
returns True
.
is With Integers and Strings
For small integers and short strings, Python may cache the objects and reuse them, making is
comparisons sometimes return True
. However, relying on this behavior is not recommended because it’s inconsistent, and you should use ==
instead.
Example:
x = 256 y = 1000 print(x is 256) print(y is 1000)
Output:
True False
In the example above, since Python caches small integers x is 256
is True
(line 3). However, for larger integers, such as 1000, new objects are created, and y is 1000
is False
(line 4).
is Operator Usage Best Practices
As is evident from the discussion in the sections above, the is
operator is best used with singleton values or to check whether two variables hold the same object. Otherwise, it should not be used as an alternative for checking whether two values are equal.
The is not Operator
The is not
operator is the same as the is
operator, except that it’s the exact Boolean opposite of if. It returns False
when two references point to the same object and True
when they don’t.
Example:
a = [1, 2, 3] b = a c = [1, 2, 3] print(a is not b) print(a is not c)
Output:
False True
Summary & for Python is Operator
The is
operator in Python checks whether two references point to the same object in memory and returns True
if they are and False
otherwise. It is distinct from the equality operator ==
, which checks whether the values of two objects are equal.
The basic syntax for the is
operator is:
obj1 is obj2
Use is
to check whether two references point to the same object.
python a = [1, 2, 3] b = a a is b # --> True
Comparing different objects with the same content returns False
.
python a = [1, 2, 3] c = [1, 2, 3] a is c # --> False
For singletons such None
, True
, and False
, use is
for comparison.
python x = None x is None # --> True
Python may cache small integers and short strings objects, making a comparison using is
unreliable.
python x = 256 y = 1000 x is 256 # --> True y is 1000 # --> False
Use is
to check for singleton instances and to ensure two references are to the same object, otherwise do not use is
as a substitute for ==
when comparing values.
The is not
operator is the Boolean opposite of is
.
a = [1, 2, 3] b = a a is not b # --> False