← Back to course
Python 11-12 / Week 03 / Monday
1/6
Week 03 · Guarding the State

Monday

Design first
// Design a class first, then hide its data behind methods
⏱ about 30 min

Monday: Design First

Comet already has IDLE open. "Pip needs a class," she says. "Name, row, col, battery. I can type it in five minutes!"

Wren puts a blank sheet of graph paper over her keyboard, gently. "What does the evidence say about last week?" he asks. "Sample went smoothly because you sketched it first."

Nova projects an empty box with three sections: a name, the attributes and the behaviors. "How can I help?" she asks. "Fill the box before you type. Code is easier to change on paper."

Comet laughs and takes the pencil from behind her ear. "Fine. Lead programmer, what does a rover need to know, and what does it need to do?"

Design before you code

Before you write a class, it helps to design it: its attributes and its behaviors. The design can be in plain words or a diagram.

Abstraction reduces complexity by focusing on the main idea. Pip has wheels, motors and a camera, but FieldSim keeps only what the simulation needs.

Rover (class diagram)Entries
Attributes (what it knows)name, row, col, heading, battery
Behaviors (what it does)turn_left, turn_right, report its status
Starting staterow 0, col 0, heading E, battery 100
ATTRIBUTE OR BEHAVIOR?
  • Read the question.
  • Tap your answer.
battery
turn_right
heading

Comet's first sketch in code

Comet typed a quick sketch with plain attributes anyway. Wren asks one question: what stops outside code from setting a value that makes no sense?

# rover_sketch.py
class Rover:
    def __init__(self, name, row=0, col=0):
        self.name = name
        self.row = row
        self.col = col
        self.heading = "E"
        self.battery = 100


pip = Rover("Pip")
print(pip.name, pip.row, pip.col, pip.heading, pip.battery)
pip.battery = -40
pip.heading = "up"
print(pip.battery, pip.heading)
PREDICT ROVER_SKETCH.PY
  • Read the question.
  • Tap your answer.
What is the first line?
What is the second line?
Pip 0 0 E 100
-40 up

No error, just a rover in a state that cannot happen on the field. That is a logic error waiting to spread. Tomorrow the crew hides the state behind methods.

DESIGN A CLASS
  • ?Write the code from the design
  • ?List what each object knows (attributes)
  • ?Decide the starting state
  • ?List what each object does (behaviors)
  • ?Name the class
WHY THIS EXERCISEDesigning the attributes and behaviors first, in words or a diagram, makes the code easier to write and check.
At your computer
1. On paper, copy the Rover class diagram. Add one attribute and one behavior of your own.
2. Type rover_sketch.py, save it in your fieldsim folder and run it.
3. Add pip.row = 99 before the last print, and print pip.row. Predict, then run it.
4. Remove the colon after class Rover. Run it, read the message, then fix it.
StatementTrue or false?
It helps to design a class before writing it.?
A design can be in plain words or a diagram.?
rover_sketch.py stopped with an error when battery became -40.?
Abstraction keeps every detail of the real thing.?
WHY THIS EXERCISEDesign comes first, abstraction keeps only the main idea, and the sketch let a bad value in without a message.
In rover_sketch.py, which attribute did outside code set to -40?
READ THE BAD HEADING
  • Read the question.
  • Tap your answer.
Which heading did outside code give Pip in rover_sketch.py?

A good design makes the code easier. Tomorrow you will guard Pip's state.