Concepts / Understanding References and Pointers

Understanding References and Pointers

If a and b refer to the same object in memory, they are identical references to that object.

  • 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?

Key point

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.

arefers to[1, 2, 3]object onebrefers to[1, 2, 3]object tworeferencereference
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

The two operators answer different questions.

issame object?identitysame object==same value?equivalencesame datacheckschecks
What does each operator compare when variables reference the same object or separate objects with equal values?

Tracing Separate Lists

Code walkthrough
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)
print(a is b)
  1. 1
    The first list literal creates an object referred to by a.
  2. 2
    The second list literal creates a separate object referred to by b.
  3. 3
    Both objects contain the elements 1, 2, and 3, so they are equivalent.
  4. 4
    Because 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.
Output
True
False
areferencelist[1, 2, 3]breferencelist[1, 2, 3]refers torefers to
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.

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

Tracing an Alias

Code walkthrough
a = [1, 2, 3]
b = a

print(a == b)
print(a is b)

b.append(4)
print(a)
print(b)
  1. 1
    The assignment a = [1, 2, 3] creates a list object and makes a refer to it.
  2. 2
    The assignment b = a does not create a new list. It creates an alias: b refers to the object already referenced by a.
  3. 3
    Because a and b refer to the same object, a is b is True.
  4. 4
    The append operation modifies that shared list through b.
  5. 5
    The 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.
Output
True
True
[1, 2, 3, 4]
[1, 2, 3, 4]
a[1, 2, 3]a[1, 2, 3, 4]b[1, 2, 3]b[1, 2, 3, 4]shared list is modifiedobserves same object
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"hello"a"goodbye"b"hello"b"hello"a is assigned a new valueb is not reassigned
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

  • Common mistake

    Using == when the question is whether two variables refer to the same object.

    What it looks like
    a == b
    Why it fails

    The == operator checks whether the objects have the same value. Separate lists containing the same elements can make this expression True.

    Do this instead

    Use 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 like

    a is b

    Why it fails

    Two separately created list objects can contain equal data while having different identities.

    Do this instead

    Use a == b for value equivalence.

  • Common mistake

    Assuming b = a creates a separate list.

    What it looks like
    a = [1, 2, 3]; b = a; b.append(4)
    Why it fails

    The assignment creates an alias. Both names refer to the same list, so the change is visible through both names.

    Do this instead

    Decide 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 like
    a = "hello"; b = "hello"
    Why it fails

    Python can reuse string objects when possible because strings are immutable.

    Do this instead

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

Related concepts

More concepts starting with U