Sub topic 1
Names, objects, and identity
Prerequisites
This chapter assumes you know basic Python syntax: functions, classes, lists, and dictionaries. You should be able to run a script or use the interactive interpreter. Examples describe CPython, the standard Python interpreter. Details such as id() values and reference counts can differ in other implementations.
Variables are names, not boxes
In many languages a variable is a box that holds a value. In Python, a variable is a name that refers to an object. An object is a piece of memory with a type, a value, and an identity. Assignment does not copy the object. It binds a name to it.
a = [1]
b = a
b.append(2)
print(a) # [1, 2]
print(a is b) # True
c = list(a)
print(a is c) # FalseThe statement b = a gives the existing list a second name. The call b.append(2) changes the one list, so a shows the change. The call list(a) builds a new list with the same items, so c is a different object.
Identity, equality, and type
Every object has three properties.
| Property | What it is | How to read it |
|---|---|---|
| Identity | A number that is unique among objects alive at the same time. It never changes during the object's life. | `id(obj)` |
| Type | The kind of object. It decides which operations work. | `type(obj)` |
| Value | The contents. Mutable objects can change it. | depends on the type |
In CPython, id() returns the object's memory address. Two operators compare objects in different ways:
==asks whether two objects have equal values.isasks whether two names refer to the same object.
x = [1, 2]
y = [1, 2]
print(x == y) # True: equal values
print(x is y) # False: two separate objects
print(id(x) == id(y)) # FalseUse is for identity checks such as value is None. Use == to compare values.
What binding does
Several statements bind a name to an object. Each one only creates or changes a name.
- Assignment:
total = 10 - Function parameters: calling
f(a)binds the parameter to the same object thatarefers to. forloops: each pass binds the loop name to the next item.import,def, andclass: these bind a module, function, or class to a name.
Passing an argument therefore shares the object. It does not copy it.
def add_item(items):
items.append("new") # changes the caller's list
def rebind(items):
items = ["other"] # binds the local name only
data = ["old"]
add_item(data)
print(data) # ['old', 'new']
rebind(data)
print(data) # ['old', 'new']add_item changes the shared object. rebind points its local name at a new list, and the caller's list stays the same.
Common errors
- Using
isto compare values.a is bcan be true or false for equal numbers or strings depending on caching. Use==. A later chapter covers interning. - Expecting
b = ato copy. It does not. Uselist(a),a.copy(), orcopy.deepcopy(a)when you need a separate object. - Relying on
id()after an object is gone. Once an object is freed, a new object can reuse its identity.
Why this matters for memory
An object stays in memory while some name or container still refers to it. When nothing refers to it, CPython can free it. The next chapters build on this idea: how aliasing affects mutable objects, and how CPython counts references to decide when to free memory.
Exercise
After a = [1, 2], b = a, and b = b + [3], what do a and a is b show? Explain why.
Show answer
a is still [1, 2] and a is b is False. The expression b + [3] builds a new list, and the assignment binds b to it. The original list is unchanged, and a still refers to it. Using b += [3] instead would extend the shared list in place, so a would also show [1, 2, 3].
Exercise
Why is if result is None: correct, while if count is 5: is a bug?
Show answer
None is a single object, so an identity check against it is reliable. Small numbers may or may not be the same object depending on CPython caching, so count is 5 depends on an implementation detail. Use count == 5 to compare values.