Skip to content

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.

Beginner 8 min read·Lesson 248 of 306 in Python Programming

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

Python
a = [1, 2]
b = a # alias: same list
c = list(a) # new list, equal contents
print(a is b, a is c, a == c)
  • Assignment binds names, never copies.
  • is: same object; ==: equal value.
  • Containers hold references.

Code examples

Aliases share one object

Python
a = [1, 2, 3]
b = a
b.append(4)
print(a, a is b)
c = list(a)
c.append(5)
print(a, c, a is c, a == c[:4])
Output
[1, 2, 3, 4] True
[1, 2, 3, 4] [1, 2, 3, 4, 5] False True

is versus ==

Python
x = [1, 2]
y = [1, 2]
print(x == y, x is y)
n = None
print(n is None)
Output
True False
True

More examples

Shared references in containers

Python
grid = [[0] * 3] * 3
grid[0][0] = 9
print(grid)
grid = [[0] * 3 for _ in range(3)]
grid[0][0] = 9
print(grid)
registry = []
registry.append({"name": "Asha"})
user = registry[0]
user["name"] = "Asha K"
print(registry)
Output
[[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:

TextOutput
[[9, 0, 0], [9, 0, 0], [9, 0, 0]]
[[9, 0, 0], [0, 0, 0], [0, 0, 0]]
[{'name': 'Asha K'}]

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

if name is "admin":

Prefer

if name == "admin":

Two equal strings aren't guaranteed to be the same object.

Assuming assignment copies

Avoid

backup = settings
settings["theme"] = "dark" # backup changes too

Prefer

backup = settings.copy()

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

  1. 1
    Predict and then check the output of a = [1]; b = a; a = a + [2]; print(b).
  2. 2
    Create 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?
Practise this topic