Names, objects and identity
Names, objects and identity
Python is a language, described by the Python Language Reference. CPython is the implementation written in C, the python program from python.org. PyPy, Jython and IronPython are others. A statement in this journey is a language rule unless it begins with "In CPython".
Names and objects
Problem. A shop keeps the prices of a customer's cart in a list. We write prices = [5, 8]. Later we write prices = "empty". How could Python let one name hold a list first and a string later?
New words
- binding: linking a name to an object
- identity: which object it is
The following syntax template shows an assignment.
<name> = <expression>
Python evaluates the expression and creates an object. Then it binds the name to that object. Every object has an identity, a type and a value. The identity and the type never change. A name has none of the three. It only links to an object.
prices = [5, 8]
print(type(prices))
prices = "empty"
print(type(prices))
<class 'list'>
<class 'str'>
The first line created a list and bound prices to it. The third line created a string and rebound prices to it. Neither object changed. Only the link moved.
Other statements bind names too: def, class, import, the for header, as in with and except, and the parameters of a function call.
NOTE: Picture a name as a label tied to an object. One object can carry many labels.
Recall. After data = 7 and then data = [7], does the integer object 7 change, and which object does data link to?
Aliases and identity
Problem. The cart holds prices = [5, 8]. We want to keep the original prices before adding a third price, so we write backup = prices. How could we check whether backup is a second list?
An alias is a second name for the same object. The following diagram shows prices and backup after the assignment.
prices ---+
+--> [5, 8]
backup ---+
Diagram description: two arrows, one from the name prices and one from the name backup, both point at one list that holds 5 and 8.
The is operator compares identities. The id() function returns an integer for an identity. That integer stays constant for the object's lifetime, and no two live objects share it.
prices = [5, 8]
backup = prices
prices.append(3)
print(backup)
print(backup is prices)
print(id(backup) == id(prices))
[5, 8, 3]
True
True
The following trace table shows the code above, one row per line.
prices |
backup |
backup is prices |
|---|---|---|
| [5, 8] | ||
| [5, 8] | [5, 8] | True |
| [5, 8, 3] | [5, 8, 3] | True |
So backup = prices made no backup. The assignment bound a second name to the same list, and append updated that list.
The == operator compares values, using the rule of the type. Two separate lists can have equal values.
first = [5, 8]
second = [5, 8]
print(first == second)
print(first is second)
True
False
Use == to ask whether contents match. Use is to ask whether two names link to one object. Write x is None to test for None, because the program has one None object.
NOTE: Do not use is to compare numbers or strings. CPython reuses some of these objects, so is can give True for equal values. The language promises nothing here.
In CPython: id(x) is the memory address of x. Another implementation may return a counter.
Recall. After first = [1], second = first and third = [1], which of first is second, first is third and first == third are True?
Sources
- The Python Language Reference, Data model, Objects, values and types: https://docs.python.org/3.12/reference/datamodel.html
- The Python Language Reference, Execution model, Binding of names: https://docs.python.org/3.12/reference/executionmodel.html
- The Python Language Reference, Assignment statements: https://docs.python.org/3.12/reference/simple_stmts.html
- The Python Standard Library, Built-in Functions: https://docs.python.org/3.12/library/functions.html
Cards
In total = 3, which of identity, type and value does the name total have?
Reveal
None of the three. A name only links to an object. The integer object 3 has an identity, a type and a value.
Which two properties of a Python object never change during its lifetime?
Reveal
Its identity and its type. Its value can change if the object is mutable.
After these lines, what does label link to, and what does type(label) show?
label = "ab"
label = 12
Reveal
label links to the integer object 12, and type(label) shows <class 'int'>. The string object "ab" did not change. Only the link moved.
Besides =, name four Python statements or constructs that bind a name.
Reveal
Any four of def, class, import, the for header, as in with and except, and the parameters of a function call.
After saved = cart for a list cart, how many lists exist, and what is saved?
Reveal
One list exists. saved is an alias, a second name for the same list. The assignment copies nothing.
What do p is q and p == r give after these lines?
p = ["x"]
q = ["x"]
r = q
Reveal
p is q gives False, because p and q link to two separate lists. p == r gives True, because == compares values and both lists hold "x".
Which test checks whether x is None, and why does it use is?
Reveal
x is None. The program has one None object, so an identity test is enough.
Why should you avoid comparing numbers or strings with is in Python?
Reveal
CPython reuses some of these objects, so is can give True for equal values. The language promises nothing here. Use == to compare values.
In CPython, what does id(x) return, and what does the language itself promise about it?
Reveal
In CPython it returns the memory address of x. The language only promises an integer that stays constant for the object's lifetime and that no two live objects share. Another implementation may return a counter.