Skip to content

How to Check If Variables Refer to the Same Object in Python

Learn how to use Python's is operator and id() function to determine if two variables reference the same object in memory, and understand the key difference between identity and equality.

In Python, everything is an object, and variables are simply references (or names) pointing to those objects in memory. A common source of bugs involves confusing equality (do these two objects contain the same data?) with identity (are these two variables pointing to the exact same object in memory?).

This guide explains how to determine if two variables refer to the same object using the is operator and the id() function, and clarifies the crucial difference between identity and equality.

Understanding Object Identity

Every object in Python has a unique identity. In the standard CPython implementation, this identity corresponds to the object's memory address.

  • Variable: A name/label that points to an object.
  • Object: The actual data stored in memory.
  • Identity: The unique address where that data lives.

If two variables refer to the same object, modifying one (if the object is mutable, like a list) will modify the other.

The most Pythonic and efficient way to check for identity is using the is operator. It evaluates to True if two variables point to the same memory address, and False otherwise.

Example about checking references:

list_a = [1, 2, 3]

list_b = list_a

list_c = [1, 2, 3]

print(f"list_a is list_b: {list_a is list_b}")
print(f"list_a is list_c: {list_a is list_c}")

Output:

list_a is list_b: True
list_a is list_c: False

Method 2: Using the id() Function (Debugging)

The built-in id(obj) function returns the integer representing the object's identity (memory address in CPython). You can compare these integers manually, though is is preferred for readability.

This method is useful for debugging to "see" where objects are living.

x = {"name": "Alice"}
y = x
z = {"name": "Alice"}

print(f"ID of x: {id(x)}")
print(f"ID of y: {id(y)}")
print(f"ID of z: {id(z)}")

if id(x) == id(y):
    print("x and y refer to the same object")

Output (IDs will vary on every run):

ID of x: 138057828456768
ID of y: 138057828456768
ID of z: 138057828465408
x and y refer to the same object

Identity vs. Equality (is vs ==)

It is vital to distinguish between:

  • is (Identity): Do they point to the same place?
  • == (Equality): Do they have the same value?

The Confusion

A common error is using is to check for value equality, or assuming that == implies is.

list_1 = [10, 20]
list_2 = [10, 20]

print(f"list_1 is list_2: {list_1 is list_2}")

print(f"list_1 == list_2: {list_1 == list_2}")

Output:

list_1 is list_2: False
list_1 == list_2: True

Common Pitfalls: Interning and Caching

Python performs optimizations (interning) for small integers and string literals. This means Python might automatically make two variables point to the same object to save memory, even if you didn't explicitly alias them.

Integer Caching

Python typically caches integers from -5 to 256.

a = 100
b = 100
print(f"100 is 100? {a is b}") # True (Optimization)

x = 1000
y = 1000
print(f"1000 is 1000? {x is y}") # Often False, but can be True in script files

Output:

100 is 100? True
1000 is 1000? False

Conclusion

To determine if two variables refer to the same object:

  1. Use a is b as the standard, efficient check.
  2. Use id(a) == id(b) if you need to visualize the memory addresses for debugging.
  3. Remember: == checks values, while is checks reference/memory.
  4. Be aware of Python's internal caching for small numbers and strings, which can sometimes make distinct assignments share an identity.
Navigation