A short Python program goes on the board. Before anyone touches a
keyboard you commit to a prediction — the exact output, in ink. Then
we run it. That much is unchanged from Grade 11. What changed is the
programs: they now contain objects, and objects can be shared. The
question is no longer only “what will print” but “how many things are
actually here”.
How to run it
Read the program twice, silently. No talking yet.
Before predicting the output, sketch the objects. How many lists
exist? How many of them does each object hold?
Write the exact output you expect, character for character —
brackets, quotation marks, spacing, number of lines.
Compare with a neighbour. The code settles disputes, not volume.
Run it. The gap between your prediction and Python’s answer is
today’s lesson.
Most rooms split three ways. ['Ali', 'Bea'], because nothing was
appended to the juniors. ['Ali', 'Bea', 'Cy'], because something
sneaky is going on. A crash, because surely you cannot do that.
What Python actually prints
['Ali', 'Bea', 'Cy']
There is only one list in this program. roster names it,
seniors.members names it, and juniors.members names it — three
names, one object. Appending through any of those names changes the
thing all three refer to. Ask Python directly and it agrees:
print(seniors.members is juniors.members)
That prints True. The word is asks “same object?”, not merely
“equal contents?“.
The fix is to give each team its own list, by copying at the door:
self.members = list(members). Now there are two lists and the
surprise is gone.
One variation
Reverse it. Show only the output and let pairs write a program that
produces it. At this level the interesting version is: produce
['Ali', 'Bea', 'Cy'] twice from two different objects — once
because they genuinely share a list, once because they hold separate
lists that happen to match. The two programs behave identically today
and diverge the moment somebody appends. That difference is the whole
point of Objects and Classes.
Predict the state, not just the printout
A printed line is one frame of a film. The prediction worth writing
down is what every object holds after the program has run. Half
the bugs in The Software Project will be a value that two
objects were quietly sharing, and the print statement that finally
revealed it will look exactly like this warm-up.
Curriculum connection
A1.1
demonstrate the ability to use integer division and resultant remainders in computer programs;
demonstrate an understanding of type conversion (e.g., string-to-integer, character-to-integer, integer-to-character, floating point-to-integer, casting in an inheritance hierarchy);