Python object and reference semantics
Chapter 41 of 132 · Level 3 · Python fluency · Python demonstrations with recorded outputs. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Why this concept matters
The outer list was copied but the inner lists are still shared. Equality compares values; is checks whether two names refer to the very same object. Nested mutation can therefore surprise you.
Learning goal: Python object and reference semantics. Start by reading what the program does, then inspect its exact output. Where an example illustrates only a prerequisite, study the named advanced feature separately before using it in production.
02 · The important subtopics
Study these connected ideas as you work through the demonstration:
- Binding names to objects
- Identity equality and mutability
- Shallow versus deep copy
- Garbage collection at a conceptual level
03 · Pinpoint: read the code line by line
- Line 1
original = [['blue'], ['green']]Create, update, or use a value. Notice what values enter this statement and which names it affects. - Line 2
shallow = original.copy()Create, update, or use a value. Notice what values enter this statement and which names it affects. - Line 3
shallow[0].append('silver')Create, update, or use a value. Notice what values enter this statement and which names it affects. - Line 4
print(original[0])Display a result. Notice what values enter this statement and which names it affects. - Line 5
print(shallow is original)Display a result. Notice what values enter this statement and which names it affects.
Find the literal input values and arguments, follow each operation in execution order, and verify the result below. This habit turns a code listing into a testable explanation.
04 · Runnable Python example and verified output
Python object and reference semantics: working demonstration
Python · runnableoriginal = [['blue'], ['green']]
shallow = original.copy()
shallow[0].append('silver')
print(original[0])
print(shallow is original)
['blue', 'silver'] False
Why it works: The outer list was copied but the inner lists are still shared. Equality compares values; is checks whether two names refer to the very same object. Nested mutation can therefore surprise you.
Important: Do not use is to compare ordinary string or number values; use ==.
Worked example 2 · Python Object And Reference Semantics in practice
Python · executed and checkeda = [1, 2]
b = a.copy()
b.append(3)
print('Original:', a)
print('Copy:', b)
Original: [1, 2] Copy: [1, 2, 3]
Pinpoint learning: This practical python object and reference semantics demonstration uses a small input you can inspect. Run it exactly as shown, compare the displayed result, then change one value and predict how the result will change.
Understand the code line by line
1: a = [1, 2]— Store or update the value on the left using the expression on the right.2: b = a.copy()— Store or update the value on the left using the expression on the right.3: b.append(3)— Execute this statement and observe its effect on later values.4: print('Original:', a)— Display the value so you can compare it with the verified output.5: print('Copy:', b)— Display the value so you can compare it with the verified output.
Worked example 3 · Change one input and compare
Python · executed and checkeda = [3, 2]
b = a.copy()
b.append(3)
print('Original:', a)
print('Copy:', b)Original: [3, 2] Copy: [3, 2, 3]
Pinpoint learning: Exactly one literal input was changed from the preceding demonstration. Compare the outputs: identify the modified value, trace the operation, and explain which result changed. The displayed output was produced by executing this variant.
Understand the code line by line
1: a = [3, 2]— Store or update the value on the left using the expression on the right.2: b = a.copy()— Store or update the value on the left using the expression on the right.3: b.append(3)— Execute this statement and observe its effect on later values.4: print('Original:', a)— Display the value so you can compare it with the verified output.5: print('Copy:', b)— Display the value so you can compare it with the verified output.
05 · Independent practice and answer guidance
Exercise A · Adapt the code
Use copy.deepcopy to make a fully independent nested list and verify that changing it leaves the first untouched.
Exercise B · Predict
Before pressing Run, predict the first displayed line. Next, change one literal value and record the new observed output. Explain the difference in one clear sentence.
Exercise C · Debug
Make a temporary invalid change, read the exception or failed assertion if one occurs, restore the working code, and explain the repair.
Worked explanation for the original example
The outer list was copied but the inner lists are still shared. Equality compares values; is checks whether two names refer to the very same object. Nested mutation can therefore surprise you.
['blue', 'silver'] False
06 · Build, verify and advance
Real-world task: Visualize shared lists and fix an unintended mutation bug.
Run the original code; explain the important lines; change one input and verify the new outcome; use a test or recorded result to justify your conclusion.
