Skip to main content

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

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:

# Create a list object
list_a = [1, 2, 3]

# Assign list_b to list_a (creates a reference, not a copy)
list_b = list_a

# Create a new list with the same content
list_c = [1, 2, 3]

# ✅ Check identity
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
note

In this example, list_a and list_b are aliases for the same object. list_c contains the same data but lives at a different memory address.

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"}

# ✅ Inspecting Memory Addresses
print(f"ID of x: {id(x)}")
print(f"ID of y: {id(y)}")
print(f"ID of z: {id(z)}")

# Comparing IDs directly
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]

# ⛔️ Incorrect Assumption: They look the same, so they must be the same object
# This checks IDENTITY
print(f"list_1 is list_2: {list_1 is list_2}")

# ✅ Correct: To check if they contain the same VALUES
# This checks EQUALITY
print(f"list_1 == list_2: {list_1 == list_2}")

Output:

list_1 is list_2: False
list_1 == list_2: True
warning

Never use is to compare values (like if user_input is "yes"). Always use == for values. Only use is when you specifically need to check if two variables share the same memory reference (common when checking for None).

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.

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

# Larger integers (Not always cached, depends on implementation)
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
tip

Do not rely on is for integers or strings unless you are strictly checking against singletons like None, True, or 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.