id() is a built-in Python function that returns a unique integer identifier for any object — essentially the object’s memory address during its lifetime. This ID is constant for the object’s lifetime but can change between Python sessions or interpreter restarts. In 2026, id() remains a fundamental debugging and introspection tool in data science (checking object identity in pandas/Polars/Dask), software engineering (memory leak detection, caching diagnostics), and metaprogramming (object identity checks, weak references) — helping distinguish between two objects being the same (identity) versus having the same value (equality with ==).
Here’s a complete, practical guide to using id() in Python: basic identity checks, mutable vs immutable behavior, real-world patterns (earthquake DataFrame debugging, caching validation, object tracking), and modern best practices with type hints, performance, pitfalls, and integration with Dask/Polars/pandas/NumPy/weakref.
Basic id() — object identity vs equality.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(id(a)) # e.g., 140723234912016
print(id(b)) # different ID
print(id(c)) # same as a
print(a == b) # True (same content)
print(a is b) # False (different objects)
print(a is c) # True (same object)
# Immutable types often share IDs for small values (implementation detail)
x = 42
y = 42
print(id(x) == id(y)) # Usually True (Python interns small ints)
Mutable vs immutable behavior — when IDs change or stay the same.
# Immutable: new object on modification
s1 = "hello"
s2 = s1
print(id(s1) == id(s2)) # True
s1 += " world" # creates new string
print(id(s1) == id(s2)) # False now
# Mutable: same ID after modification
lst = [1, 2, 3]
lst_id = id(lst)
lst.append(4)
print(id(lst) == lst_id) # True (modified in place)
Real-world pattern: debugging & validating earthquake DataFrame operations — identity checks.
import pandas as pd
import dask.dataframe as dd
df = pd.read_csv('earthquakes.csv')
print(f"Original ID: {id(df)}")
# In-place operation (same ID)
df.sort_values('mag', inplace=True)
print(f"After sort (inplace): {id(df)}") # same
# New object created
sorted_df = df.sort_values('mag')
print(f"Sorted copy ID: {id(sorted_df)}") # different
# Dask: lazy operations create new objects
ddf = dd.from_pandas(df, npartitions=4)
print(f"Dask ID: {id(ddf)}") # new Dask object
ddf_filtered = ddf[ddf['mag'] >= 7.0]
print(f"Filtered Dask ID: {id(ddf_filtered)}") # new lazy object
# Persist keeps same identity but computes data
ddf_persisted = ddf.persist()
print(f"Persisted ID: {id(ddf_persisted)}") # same as ddf
Best practices for id() in Python & data workflows. Use is / is not — for identity comparison; == / != for value equality. Modern tip: use Polars/Dask — identity checks are rare in distributed data; focus on content equality. Use id() for debugging — track object identity in pipelines. Use id() in caching — verify same object reused. Add type hints — def same_object(a: Any, b: Any) -> bool: return id(a) == id(b). Use id() with weakref — track objects without preventing GC. Avoid relying on ID stability — IDs can change across sessions or implementations. Use id() in __repr__ — return f"<{self.__class__.__name__} at 0x{id(self):x}>". Use id() for memoization — custom caches. Use id() in logging — logger.debug(f"Object ID: {id(obj):x}"). Use id() in tests — assert same object: assert id(df) == id(df_sorted). Use sys.getrefcount(id(obj)) — check reference count. Use gc.get_referrers(obj) — inspect referrers. Use obj.__class__ — type checking. Use vars(obj) — inspect attributes. Use dir(obj) — list attributes. Use inspect.getmembers(obj) — detailed inspection.
id(obj) returns the unique integer identifier (memory address) of an object — use for identity checks (is), debugging, caching validation, and object tracking. In 2026, use with is/is not for identity, == for equality, and integrate with pandas/Polars/Dask for pipeline debugging. Master id(), and you’ll gain deep insight into object identity, memory behavior, and data flow in any Python project.
Next time you need to know if two variables point to the same object — use id(). It’s Python’s cleanest way to say: “Are these the exact same thing in memory? — show me the address.”