University of Melbourne · FACULTY OF COMPUTER SCIENCE

COMP90059 Chap.7 Tuples, Mutability and Debugging

- one subject, every graph, every model, every mark
12 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 7 of 9 · COMP90059

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.

In this chapter

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

Q [5 marks]. Sort job records by decreasing priority and then increasing distance. The marks used for these steps are a revision aid, not an official marking scheme.
Aliases are two routes to the same list.Aliases are two routes to the same list. Element assignment changes the object at the shared destination.abone list object[price, quantity, status]
Aliases are two routes to the same list. Element assignment changes the object at the shared destination.
  • 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.
A key of negative priority followed by positive distance encodes the two directions independently. Reverse applied to both fields would order tied distances incorrectly.
Sia tip — Write the key tuple beside every record before reading the final order.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 177 of your University of Melbourne subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMP90059 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works