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.
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.
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?
review[0] = 15Python 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.
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 program prepares several versions of a product catalogue. Run the following Python statements in order, then evaluate the six checks without changing any list.
original = [16, 24]
linked = original
independent = [16, 24]
copied = original.copy()- original is linked
- original == independent
- original is independent
- original is copied
- independent == copied
- linked is copied
Complete the table using the exact words True or False.
| Check | Result |
| Check 1 | |
| Check 2 | |
| Check 3 | |
| Check 4 | |
| Check 5 | |
| Check 6 |
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.