Understanding References and Pointers
Identity (is) checks whether two variables refer to the same object in memory; equivalence (==) checks whether two objects have the same value.
Try it: Names and Objects
How Python variables are names bound to objects: assignment never copies, mutating a list is seen through every name that points at it, and ints are replaced rather than changed.
How it works
- name = object binds a name; b = a makes b point at the same object.
- Mutating a list (append, +=) changes the one shared object.
- a = a + [x] and y = y + 1 create NEW objects and re-point one name.
- Passing a list to a function passes the reference, so the function can change it.
Default run (7 steps): Two names, one list. Every name is a label that points at an object. … Printed: [1, 2, 3] / True
Simplified: Six fixed scripts on a tiny heap; you choose the values. Object numbers are illustrative, not real id() values.
Loading the simulation…
The Backup That Changes
Imagine assigning a list of scores to one variable and then assigning that variable to another name as a supposed backup. If you modify the list through the first name, the second name can show the same modification. The reason is that the two variables may refer to one shared list object rather than two independent lists. To understand this behavior, you must separate two questions: do the objects contain the same data, and are the variables referring to the very same object?
Identity asks whether two variables refer to one object. Equivalence asks whether the objects have the same value.
Two Ways to Be the Same
Object identity describes sameness of object. If a and b refer to the same object in memory, they are identical references to that object. Value equivalence describes sameness of data. If a and b refer to separate objects that contain the same data, they are equivalent even though they are not identical. Python provides different operators for these questions: is tests identity, while == tests value equivalence.
| Question | Operator | What is compared? |
|---|---|---|
| Do both variables refer to the same object? | is | Object identity |
| Do the objects contain the same data? | == | Value equivalence |
Tracing Separate Lists
a = [1, 2, 3] b = [1, 2, 3] print(a == b) print(a is b)
The two lists are equivalent because they contain the same elements. They are not identical because each list literal creates a separate list object. This is the central case where == produces True while is produces False.
Tracing an Alias
a = [1, 2, 3] b = a print(a == b) print(a is b) b.append(4) print(a) print(b)
Strings and Lists
Strings and lists illustrate why identity behavior depends on the kind of object. Strings are immutable: they cannot be changed after creation. Python can therefore reuse string objects when possible. Lists are mutable: their contents can be changed, so Python creates separate list objects when two list literals contain the same elements. This prevents a change made through one independently created list from silently changing another independent list.
Reassigning a string variable gives that variable a reference to a string with the new value; it does not modify the original string object. This differs from mutating a list, where an operation such as append changes the shared list object itself.
Common Reference Mistakes
Using == when the question is whether two variables refer to the same object.
The == operator checks whether the objects have the same value. Separate lists containing the same elements can make this expression True.
Fix:
Use a is b when the question is object identity.Using is when the question is whether two objects contain the same data.
Two separately created list objects can contain equal data while having different identities.
Fix:
Use a == b for value equivalence.Assuming b = a creates a separate list.
The assignment creates an alias. Both names refer to the same list, so the change is visible through both names.
Fix:
Decide explicitly whether shared data or an independent copy is intended.Assuming equal string values always prove that two variables have independent objects.
Python can reuse string objects when possible because strings are immutable.
Fix:
Do not infer identity from value alone; use is when identity is the actual question.
Check the Reference
What do you think happens?
What do you predict these expressions print?
Reveal answer
Answer: True, True, [1, 2, 3, 4], [1, 2, 3, 4]
b = a creates an alias rather than a new list. Therefore both variables refer to one list, both comparisons return True, and the append operation changes the shared object observed through both names.
Create two separate list literals with the same elements. Before running the code, predict the results of == and is. Then assign one variable to the other, predict again, and explain why the identity result changes.
Hints
- A list literal creates a list object.
- Assigning one variable to another creates an alias.
- Ask separately whether the data is equal and whether the object is shared.
The Reference Rule
- Use is to test whether two variables refer to the same object.
- Use == to test whether two objects have the same value.
- Identity implies equivalence because one shared object has one shared value.
- Equivalence does not imply identity because separate objects can contain the same data.
- With mutable lists, an alias allows changes through one variable to appear through the other; immutable strings cannot be changed in place.
Key Takeaways
- Identity and value equivalence are different questions.
- The is operator checks shared object identity, while == checks equal values.
- If two variables refer to one object, they are equivalent; equal values alone do not prove shared identity.
- b = a creates an alias, so mutations to a shared list are visible through both variables.
- Strings are immutable and may be reused, while separately created lists are mutable and are treated as separate objects.