Module 17 · Advanced Python Concepts — topic 2 of 14
References
How Python names refer to objects: assignment never copies, aliases share one object, is versus ==, references inside containers, and the classic [[0] * 3] * 3 trap.
Learning objectives
- Explain names as references to objects
- Distinguish identity (is) from equality (==)
- Avoid accidental sharing in nested structures
Definition
In Python, variables are names bound to objects. Assignment makes a name refer to an object; it never copies the object. Several names (and container slots) can refer to the same object at once.
Introduction
b = a doesn't make a second list — it gives the same list a second name. Changing it through b changes it for a too.
Why it matters
Understanding references explains when data is shared, prevents spooky action-at-a-distance bugs, and makes copying deliberate.
Detailed explanation
An assignment statement binds a name to an object. b = a makes b refer to the same object as a; this is called aliasing.
Mutating the object through any alias is visible through all of them. Rebinding one name (b = something_else) doesn't affect the others.
is checks identity (the same object); == checks equality (equal values). Two different lists can be equal but not identical.
Use is for singletons: x is None, x is True. Use == for comparing values.
id(obj) returns a number identifying the object during its lifetime; a is b is the same as id(a) == id(b).
Containers store references. [[0] * 3] * 3 creates one inner list referenced three times, so changing one row changes all of them.
list(a), a[:] and a.copy() create new lists, but the items are still shared references (see Shallow copy).
Function calls work the same way: the parameter becomes another name for the argument object, which is why functions can modify mutable arguments.
Syntax
- Assignment binds names, never copies.
- is: same object; ==: equal value.
- Containers hold references.
Code examples
Aliases share one object
[1, 2, 3, 4] True [1, 2, 3, 4] [1, 2, 3, 4, 5] False True
is versus ==
True False True
More examples
Shared references in containers
[[9, 0, 0], [9, 0, 0], [9, 0, 0]]
[[9, 0, 0], [0, 0, 0], [0, 0, 0]]
[{'name': 'Asha K'}]Step-by-step explanation
- Multiplying a list of lists copies the reference to the same inner list three times.
- The comprehension creates a new inner list on each iteration, so rows are independent.
- user is just another name for the dictionary stored in registry, so editing it edits the registry entry.
Expected output
Running “Shared references in containers” prints exactly:
Practical applications
- Game boards and matrices built from nested lists.
- Caches and registries returning shared objects.
Best practices
- Use is only for None, True, False and identity checks.
- Build nested lists with comprehensions.
- Copy explicitly when you need independence.
Common mistakes
Using is for value comparison
Avoid
Prefer
Two equal strings aren't guaranteed to be the same object.
Assuming assignment copies
Avoid
Prefer
Assignment creates an alias, not a backup.
Important notes
- Python Tutor (pythontutor.com) visualises names and references step by step.
Key takeaways
- Names are references to objects.
- Aliases share mutations; rebinding doesn't.
- is compares identity, == compares values.
Try it yourself
- 1Predict and then check the output of a = [1]; b = a; a = a + [2]; print(b).
- 2Create a 4×4 board correctly and place a piece without affecting other rows.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhat is printed?