Sub topic 1
Names refer to objects: what binding means
Before you can predict which variable a function sees, you need a precise picture of what a variable is in Python. This chapter builds that picture. Later chapters on namespaces and the LEGB rule rely on it.
Prerequisites
This chapter assumes you can:
- define and call a function in Python
- use assignment and
print - work with lists and basic data types such as
intandstr - read a simple traceback
A name is a label, not a box
In many languages a variable is a box that holds a value. In Python it is closer to a label attached to an object. The statement x = 5 does two things. It makes sure an object representing 5 exists, and it binds the name x to that object.
Binding is the act of associating a name with an object. The object lives on its own. Several names can point to the same object, and a name can later be moved to a different object.
a = [1, 2, 3]
b = a
print(a is b)
print(id(a) == id(b))
# Output:
# True
# TrueThe is operator checks whether two names refer to the same object. The id() function returns an integer that identifies an object while it exists. Here b = a does not copy the list. It adds a second label to the same list.
Rebinding versus mutating
Two different operations can change what you see through a name.
Mutation changes the object itself. Every name bound to that object sees the change.
a = [1, 2, 3]
b = a
b.append(4)
print(a)
# Output: [1, 2, 3, 4]Rebinding moves a name to a different object. Other names are unaffected.
a = [1, 2, 3]
b = a
b = [9, 9]
print(a)
print(b)
# Output:
# [1, 2, 3]
# [9, 9]The line b = [9, 9] creates a new list and points b at it. The original list is still bound to a. This distinction returns in the chapter on mutating a global object versus rebinding its name.
Many statements bind names
Plain assignment is only one way to bind a name. These statements also create bindings:
| Statement | Name it binds |
|---|---|
| `x = 1` | `x` |
| `x += 1` | `x` (rebinds it) |
| `for item in data:` | `item` |
| `def greet(): ...` | `greet` |
| `class Box: ...` | `Box` |
| `import math` | `math` |
| `with open(p) as f:` | `f` |
| `except ValueError as e:` | `e` |
Each of these puts a name in a particular scope, which is a region of code where that name is visible. The next chapters explain how Python decides which region.
Augmented assignment on immutable objects
Integers, strings, and tuples are immutable, meaning their contents cannot change after creation. So n += 1 on an integer cannot modify the integer. It computes a new integer and rebinds the name.
n = 10
m = n
n += 1
print(n)
print(m)
# Output:
# 11
# 10For a list, += behaves differently. It extends the existing list in place and then rebinds the name to that same object.
a = [1]
b = a
a += [2]
print(b)
# Output: [1, 2]Why this matters for scope
Scope rules are rules about names, not about objects. When Python meets a name, it asks which namespace holds that binding. Whether an assignment creates or changes a binding decides which namespace is involved. Keep the model of "labels pointing at objects" in mind as you read on.
Exercise
After running x = [1], y = x, y = y + [2], what do x and y contain, and why?
Show answer
x is [1] and y is [1, 2]. The expression y + [2] builds a new list, and y is rebound to it. The original list stays bound to x, so it is unchanged.
Exercise
Why does a = [1], b = a, b += [2], print(a) print [1, 2]?
Show answer
For lists, += extends the existing object in place. a and b are bound to the same list, so both see the change.