Understanding References and Pointers
If a and b refer to the same object in memory, they are identical references to that object.
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 |
The two operators answer different questions.
Tracing Separate Lists
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)- 1The first list literal creates an object referred to by a.
- 2The second list literal creates a separate object referred to by b.
- 3Both objects contain the elements 1, 2, and 3, so they are equivalent.
- 4Because the variables refer to different list objects, they are not identical.
- a creates one list object.
- b creates another list object with the same elements.
- == compares the list values, so the result is True.
- is compares the object identities, so the result is False.
True
FalseThe 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)- 1The assignment a = [1, 2, 3] creates a list object and makes a refer to it.
- 2The assignment b = a does not create a new list. It creates an alias: b refers to the object already referenced by a.
- 3Because a and b refer to the same object, a is b is True.
- 4The append operation modifies that shared list through b.
- 5The modification is visible through a because a still refers to the same object.
- a creates one list object.
- b = a gives b a reference to that same object.
- Both comparisons return True because the references share one object.
- append changes the shared list.
- Reading through either variable shows the changed list.
True
True
[1, 2, 3, 4]
[1, 2, 3, 4]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
Common mistake Using == when the question is whether two variables refer to the same object.
What it looks likea == bWhy it failsThe == operator checks whether the objects have the same value. Separate lists containing the same elements can make this expression True.
Do this insteadUse a is b when the question is object identity.
Common mistake Using is when the question is whether two objects contain the same data.
What it looks likea is b
Why it failsTwo separately created list objects can contain equal data while having different identities.
Do this insteadUse a == b for value equivalence.
Common mistake Assuming b = a creates a separate list.
What it looks likea = [1, 2, 3]; b = a; b.append(4)Why it failsThe assignment creates an alias. Both names refer to the same list, so the change is visible through both names.
Do this insteadDecide explicitly whether shared data or an independent copy is intended.
Common mistake Assuming equal string values always prove that two variables have independent objects.
What it looks likea = "hello"; b = "hello"Why it failsPython can reuse string objects when possible because strings are immutable.
Do this insteadDo 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
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
- 1Use is to test whether two variables refer to the same object.
- 2Use == to test whether two objects have the same value.
- 3Identity implies equivalence because one shared object has one shared value.
- 4Equivalence does not imply identity because separate objects can contain the same data.
- 5With 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.