Mutation versus rebinding trace
Independent Mastery Check — no AI assistance during the assessed work. If you receive any help, declare it honestly; assisted work counts only as remediation, and you must complete a fresh unassisted attempt before claiming mastery.
Prerequisites
Day 04 value-and-binding check completed; you can save a short Python file and run it from the terminal.
Learning Objective
Predict, run, and explain why append changes a list that two names share, while a = a + [2] rebinds only the name a.
Instructions
Required work:
1. Before running anything, write your prediction of both printed lines in the draft area.
2. Save the program from the exercise as trace.py and run python trace.py.
3. Copy the exact output next to your prediction and mark every mismatch.
4. For each case, explain in one or two sentences which list object a and b refer to after the third line of that case.
Optional work: once the required evidence is complete, add print(id(a), id(b)) after each case and confirm that the two ids match only in case 1.
Practice & Prediction Log
Predict the output, then run:
a = [1]
b = a
a.append(2)
print("case 1:", a, b, a is b)
a = [1]
b = a
a = a + [2]
print("case 2:", a, b, a is b)
Why does b change in case 1 but not in case 2?
a.append(2) modifies the list object that both names refer to. a + [2] builds a brand-new list, and a = ... points only the name a at it; b still refers to the old list.
Output (identical on Python 3.13 and 3.14):
case 1: [1, 2] [1, 2] True
case 2: [1, 2] [1] False
Explanation:
- Case 1: b = a makes both names refer to one list. append mutates that list in place, so both names show [1, 2] and a is b is True.
- Case 2: a + [2] creates a new list [1, 2] and the assignment rebinds a to it. b still refers to the original [1], so a is b is False.
Rule: mutation changes an object, and every name that refers to it sees the change; rebinding changes which object one name refers to, and other names are unaffected.
Objective Knowledge Check & Diagnostic Trace
Answer the following questions to verify your understanding. Select your declared assistance condition before submitting.
After running a = [1], then b = a, then a = a + [2], what does print(b) show?
a + [2] returns a new list, and the assignment points only a at it. b still refers to the original list [1]. Only an in-place operation such as append would be visible through b.
Ask whether the third line changes an existing object or creates a new one.
Attempt History & Evidence
Without AI assistance, you recorded a prediction before running, the exact two output lines, and a correct explanation that distinguishes mutating a shared object (case 1) from rebinding one name to a new object (case 2).
Authoritative Source Resources & Provenance
Python Standard Library: Built-in Types
Relevant Section / Range: Sections Truth Value Testing, Comparisons, and Mutable Sequence Types
Verified Version & Date: Python 3.12 documentation (verified 2026-10-30)
Normative reference for truthiness, comparison, binding, and mutation behavior.
Day 03 recovery drill
Run the included Python examples and consult the installed interpreter help.
Compared Candidate Sources & Selection Analysis (2)
Deeper object-semantics reference than the expression-level drills need; stdtypes keeps the operator-level material in scope.
Narrative introduction to the same operators; the library reference was selected as the normative signature source.
Git User Manual: Recording Changes
Relevant Section / Range: Recording Changes to the Repository
Verified Version & Date: Verified 2026-10-30
Primary reference for inspecting a small diff before a safe commit.
Basic Git awareness
Use git diff and git status locally; do not commit until the diff is understood.
Compared Candidate Sources & Selection Analysis (2)
Command-level flag reference; the Pro Git chapter was selected for its guided walkthrough of inspecting changes before committing.
Friendly commercial walkthrough; the book chapter provides deeper staging/index semantics for the same inspection habit.