← Back to course
Python 11-12 / Week 02 / Mission Quest
6/6
Week 02 · Classes and Objects

Mission Quest

Mission Quest: Blueprint cards
// A class is a blueprint, and each object keeps its own state
⏱ about 30 min

Saturday: Mission Quest: Blueprint Cards

Comet has a pile of index cards and two colored pens. On the top card she writes Sample, then lists label, kind and grams.

"That is the class card," she says. "Now I fill one object card for each real sample, with real values."

Wren holds up a lamp from the shelf. "What does the evidence say?" he asks. "Could a lamp have a class card too? It has parts and it does things."

Nova casts a soft glow on the lamp. "How can I help?" she asks. "List what every lamp has, then what every lamp can do."

Comet passes you a blank card. "Find a family member, lead programmer. Pick something at home."

Class cards and object cards

An object is one instance of a class. The class is the blueprint for the attributes and behaviors every object of that type has.

A class card lists the attributes and behaviors. An object card gives one object's own values for each attribute.

CardAttributesBehaviors
Class card: Lampcolor, brightness, is_onturn_on, turn_off, describe
Object card: desk lampcolor = green, brightness = 3, is_on = False(same as the class)
Object card: hall lampcolor = white, brightness = 5, is_on = True(same as the class)
◇ BLUEPRINT AND INSTANCE CARDS
You need index cards or scraps of paper and a pen. Choose one kind of household object together, such as a chair, a mug or a backpack.
Write one class card: the class name, three or four attributes, and two or three behaviors.
Each of you fills one object card for a real one in your home, with its own value for every attribute.
Compare your object cards. Which attributes have the same value? Which are different?
Ask each other: would two identical mugs need two object cards? Why?
For a grown-up
The idea today is the difference between a blueprint, the class, and the things made from it, the objects. Everyday objects make it concrete.
Talk about it: what could go wrong if every object card could use different attributes, the way Comet's dicts could?
CHECK YOUR CARDS
  • Read the question.
  • Tap your answer.
Which card is the blueprint?
Two lamps share a class card. Do they share brightness values?
In Python, which method sets an object's starting values?

Great teamwork. Next week Pip gets a class of its own, and the crew learns to guard its state.

← Friday