Guide Python Beginner

6.2 Objects and Variable References

Why a Python variable is a label pointing at an object rather than a box holding a value, what that means for reference assignment, and why shared references to mutable objects are a common source of bugs.

3 min read

Objects, Briefly

Every value a variable can refer to — numbers, strings, functions, even classes — is an object with its own identity, type, and value, covered in full in 5.2 Python Object Model. An object lives in memory independently of any name pointing at it, and is created, used, and eventually destroyed based on how many references point to it (see 6.6 Garbage Collection).

A Variable Is a Label, Not a Box

What Is It?

A variable does not directly “contain” a value the way a box would — it holds a reference (a pointer) to an object living elsewhere in memory.

Why Does It Matter?

Assignment copies the reference, never the underlying data. Two names can point at the exact same object.

flowchart LR A["a"] --> OBJ["[1, 2, 3]\nid: 0x7f... refcnt = 2"] B["b"] --> OBJ

a = [1, 2, 3] then b = a — both names label the same object; b.append(4) mutates it, visible through a too.

>>> a = [1, 2, 3]
>>> b = a                # b now references the SAME object as a
>>> id(a) == id(b)
True

Shared References

How Is It Used?

Because b = a shares the object rather than copying it, mutating through one name is visible through every other name referencing it:

>>> a = [1, 2, 3]
>>> b = a
>>> b.append(4)
>>> a                     # mutation via b is visible through a too
[1, 2, 3, 4]

This is exactly why the distinction between mutable and immutable objects matters so much — see 5.9 Mutable vs Immutable Types for the full comparison. It’s usually a bug when it’s unexpected, and it’s the root cause covered in 6.12 Common Mistakes.

Identity vs Value

Two names can hold equal values while pointing at two different objects — this is exactly why == and is answer different questions (see 5.10 Type Checking for id()/type()/isinstance() in depth):

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b            # equal VALUE
True
>>> a is b            # NOT the same object
False

Quick Interview Answer

“A Python variable is a label pointing at an object, not a container holding it — b = a makes b point at the exact same object as a, without copying any data. If that object is mutable, mutating it through b is visible through a too, since both names reference one shared object. == compares value; is compares identity — two objects can be equal without being the same object.”

Common Mistakes

  • Assuming b = a creates an independent copy of a list or dict — it creates a second label on the same object.
  • Using is to compare values when == is what’s actually intended — is only answers “are these the exact same object,” not “do these hold equal data.”
  • Being surprised that a function call that appends to a list argument changes the caller’s list — covered fully in 6.10 Variables in Functions.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form