Example: Reference Aliasing in a Score Tracker

A teacher uses Python lists to check two quiz scores. Reference aliasing occurs when two or more names refer to the same object. An assignment between names can create this sharing without creating another list.


Run the following setup once. Then complete the parts in order, keeping the changes made in earlier parts.


Python
scores = [12, 18]
review = scores
backup = [12, 18]


Python's is operator checks whether two expressions refer to the same object. For these lists, == compares their contents instead.

a) Compare the starting references

Before changing anything, determine the results of scores is review, scores == backup, and scores is backup. Explain why the last two results can differ.


Each list literal creates a list here. The two list literals therefore create ________ separate list objects. The middle statement, review = scores, evaluates scores and binds review to that existing object; it does not construct another list.


The names scores and review point to one list, while backup points to a separate list with equal contents.


Both scores and review refer to the first list, so scores is review is True. The list referenced by backup contains the same values in the same order, so scores == backup is True. However, it is a separate object, so scores is backup is False. Equal contents do not establish shared identity.

b) Correct a score through an alias

Now execute the following statement. What are the complete contents of scores and backup afterward?


Python
review[0] = 15


Python list indexing starts at zero, so index 0 selects the first entry. Before the correction, this entry in the shared list contains ________. The statement replaces that entry with 15 in the existing list; it does not change which list review refers to.


Because scores refers to the same list, reading scores shows the correction too. The independently created list referenced by backup receives no update.


Final contents: scores is [15, 18], and backup is [12, 18].

c) Redirect one name

Finally, execute the following statements. Give the contents referenced by all three names and the result of scores is review. The method append adds one value to the end of its list.


Python
review = [15, 18]
scores.append(21)


Rebinding changes which object a name refers to. The first statement creates a new list and rebinds review to it. Rebinding one alias does not redirect the other names. Thus, scores still refers to the list corrected in part b, while backup still refers to its original separate list.


The second statement appends 21 to the list referenced by scores. Since review now refers to another object, its list receives no appended value.


Final contents: scores is [15, 18, 21], review is [15, 18], and backup is [12, 18]. The expression scores is review is False.



A Python program prepares lists of pulse counts recorded by a sensor. The method append adds one value to the end of a list, and sum totals its values.


Python
def prepare(display):
    display.append(9)
    display = [2, 4]
    display.append(7)
    return display

readings = [3, 6]
mirror = readings
prepared = prepare(mirror)
mirror.append(12)


After the entire program runs, give the following results:

  • sum(readings), in counts, as an exact integer with zero decimal places.
  • sum(prepared), in counts, as an exact integer with zero decimal places.
  • Whether readings is mirror evaluates to true or false.
Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A program prepares several versions of a product catalogue. Run the following Python statements in order, then evaluate the six checks without changing any list.


Python
original = [16, 24]
linked = original
independent = [16, 24]
copied = original.copy()


  1. original is linked
  2. original == independent
  3. original is independent
  4. original is copied
  5. independent == copied
  6. linked is copied

Complete the table using the exact words True or False.

CheckResult
Check 1
Check 2
Check 3
Check 4
Check 5
Check 6

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A debugger examines six Python variable names: a, b, c, d, e, and f. Each refers to a list containing [52, 68]. The debugger reports the following results.


Expression

Result

a is b

True

b is c

False

c is d

True

d is e

True

e is f

False

a is f

True


All referenced lists remain alive, and no references or contents change during these checks.


Find the following three counts, giving each as an exact whole number:

  • The number of distinct list objects referenced by the six names.
  • The number of unordered pairs of different names for which is returns True.
  • The number of unordered pairs of different names for which == returns True but is returns False.
Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors