Concepts / Understanding References and Pointers

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.

  • Programming
Interactive lab

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

  1. name = object binds a name; b = a makes b point at the same object.
  2. Mutating a list (append, +=) changes the one shared object.
  3. a = a + [x] and y = y + 1 create NEW objects and re-point one name.
  4. 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.

Educational simulation

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.

referencereferencearefers to[1, 2, 3]object onebrefers to[1, 2, 3]object two
How can two variables contain equal data while referring to different objects, and how can two variables refer to the same object?
QuestionOperatorWhat is compared?
Do both variables refer to the same object?isObject identity
Do the objects contain the same data?==Value equivalence
checkschecksissame object?identitysame object==same value?equivalencesame data
What does each operator compare when variables reference the same object or separate objects with equal values?

Tracing Separate Lists

a = [1, 2, 3] b = [1, 2, 3] print(a == b) print(a is b)

refers torefers toareferencelist[1, 2, 3]breferencelist[1, 2, 3]
What does memory look like when each variable points to a separate object containing equal data?

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.

impliesmay providesame objectidentitysame valueequivalenceseparate objectscan still hold equal data
Why does sharing one object guarantee the same value, while having the same value does not guarantee sharing one object?

Tracing an Alias

a = [1, 2, 3] b = a print(a == b) print(a is b) b.append(4) print(a) print(b)

shared list is modifiedobserves same objecta[1, 2, 3]a[1, 2, 3, 4]b[1, 2, 3]b[1, 2, 3, 4]
What changes when a shared list is modified through one variable, and what does the other variable observe?

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.

python
a is assigned a new valueb is not reassigneda"hello"a"goodbye"b"hello"b"hello"
What happens to the referenced object when a string variable receives a new value?

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?

  • True, True, [1, 2, 3, 4], [1, 2, 3, 4]
  • True, False, [1, 2, 3], [1, 2, 3, 4]
  • False, True, [1, 2, 3, 4], [1, 2, 3, 4]
  • False, False, [1, 2, 3], [1, 2, 3]
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.

python
MEDIUM

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

  1. Use is to test whether two variables refer to the same object.
  2. Use == to test whether two objects have the same value.
  3. Identity implies equivalence because one shared object has one shared value.
  4. Equivalence does not imply identity because separate objects can contain the same data.
  5. 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.