COMP90059 Chap.7 Tuples, Mutability and Debugging
Tuples, Mutability and Debugging
Tuples group ordered fields into records whose slots cannot be replaced, while lists remain mutable. Assignment does not automatically copy an object: two names may become aliases for the same list, so a mutation through either route is visible through both. Rebinding one name to a new object is different from changing the shared object. Shallow copies create a new outer list but can retain shared nested objects.
Record processing uses tuple unpacking to name fields and sorting keys to state ordering and tie rules. Debugging begins by reproducing the smallest failure, classifying the error, inspecting values and types, changing one cause and retesting both the failure and a previously working case.
What this chapter covers
- 01
Tuples preserve field order while preventing slot replacement
- 02
Packing creates a tuple and unpacking binds its fields
- 03
Aliases are multiple names for one object
- 04
Rebinding changes a name while mutation changes an object
- 05
Shallow copies may share nested mutable objects
- 06
Sorted returns a new list while sort mutates in place
- 07
Key functions express sort meaning and tie rules
- 08
Error families guide the first debugging question
- 09
Identity and equality answer different questions about two references
- 10
Deeply nested copying requires an explicit ownership decision
- 11
Stable sorting preserves earlier order among equal keys
- 12
Minimal failing examples separate causes from unrelated program state
Worked example · free
Sort records by mixed directions
- 1Unpack each record as job, priority and distance so the fields are explicit.
- 1Create the key (-priority, distance).
- 1Negating priority makes larger priorities sort first under ascending key order.
- 1Leaving distance positive makes shorter distances lead within a tie.
- 1Use sorted when the original record order must be preserved.
Key terms
- Tuple
- An ordered sequence whose slots cannot be rebound after the tuple is created.
- Alias
- A second name that refers to the same object as another name.
- Rebinding
- Changing which object a name refers to without necessarily changing either object.
- Shallow copy
- A new outer collection that keeps references to the same nested objects as the source.
- Sort key
- A value derived from each item and used to determine ordering.
- Regression test
- A retained test that reproduces an earlier failure and verifies that the repair remains effective.
- Object identity
- The fact that two references designate the same runtime object, which is distinct from their objects merely comparing equal in value.
- Stable sort
- A sorting guarantee that keeps items with equal keys in their original relative order, enabling reliable multi-stage ordering.
- Minimal reproducer
- The smallest input and code fragment that still demonstrates a failure, used to isolate the relevant state and assumption.
Tuples, Mutability and Debugging FAQ
Does tuple immutability make every nested object immutable?
No. The tuple slots cannot be replaced, but a slot may refer to a mutable object that can still change through another reference and remain visible through the tuple.
Why did changing one list affect another name?
The names were aliases for the same list. Assignment copied the reference, not the list contents, so mutation through either name changes the one shared object.
What is the difference between sorted and sort?
Sorted returns a new ordered list and leaves its input iterable unchanged. The list method sort changes the existing list in place and returns None to signal that mutation.
What should I do first when code fails?
Reproduce the smallest failing input, read the error family and line, then inspect the values and types involved before editing anything or guessing at a repair.
How do I decide whether a shallow copy is enough?
List which nested objects the new owner may mutate. A shallow copy is sufficient only when sharing every nested object is acceptable or those objects are immutable. Otherwise copy the required nested levels and test changes through both references.
How can a sort key express a tie-break rule?
Return a tuple whose fields appear in priority order. Python compares the first field, then moves to the next only for a tie. Transform individual fields when their direction differs, preserve the original data and test records that tie on each successive field so the ordering contract is observable.
Exam move
Draw object diagrams for aliasing, rebinding, a shallow outer copy and a nested mutation. Predict which names observe each change, then confirm in a clean notebook. Build a list of tuple records and write three sort keys: one ascending field, one descending field and one mixed-direction tie rule. Preserve the original list and verify it after sorting. For debugging, create one syntax, name, type, value and index error.
Record the operation that raised it and the broken assumption. Add a regression test after every repair. The goal is to connect error messages with object state rather than patching symptoms at random.
Working through Tuples, Mutability and Debugging in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Tuples, Mutability and Debugging question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.