←Previous page

14OBJECTS · THE MOLD

Make your own mold with class.

A class is a mold. It says what every robot made with it will know. Then you use it to make real robots.

IN REAL LIFE

A mold is not the thing.

A mold is the plan. You use it to make the real things.

A cookie cutter

The cutter is not a cookie. You use it to make cookies.

An ice tray

The tray has the shape. Water goes in, ice cubes come out.

A blank form

Every form asks for a name. Each person writes their own.

LOOK FIRST

Three new words, one at a time.

class

class Robot: starts the mold. Mold names start with a capital letter.

__init__

The mold's setup job. It runs by itself each time a new robot is made. Two _ before and two after.

self

self means “this robot itself”. self.name = name writes the name inside this robot.

STEP BY STEP

Read the mold line by line.

Take it slow. The first 4 lines are only the plan. Line 5 makes a real robot.

class Robot:Starts the mold called Robot. No robot exists yet.
def __init__(self, name):The setup job. It gets the new robot (self) and a name.
self.name = nameWrites the name inside this robot.
self.battery = 100Every new robot starts full: 100.
bolt = Robot("Bolt")Use the mold! Now a real robot exists. You do not write self: Python fills it in.
print(bolt.name)Read its data with a dot: Bolt
print(bolt.battery)Shows 100

Remember the turtle? turtle.Turtle is a mold too. t = turtle.Turtle() used it to make one turtle.

RUN IT

Make your first robot.

Guess first. Then run real Python and check.

1

Guess the result

class Robot: def __init__(self, name, color): self.name = name self.color = color rita = Robot("Rita", "green") print(rita.name) print(rita.color)

What will Python show?

robot.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change "green" to your favorite color. Then add: print(rita.name, "is", rita.color)

YOUR TURN

Mold or robot?

Try each tiny question. A wrong answer becomes something to review.

0Completed
8Not tried
0Review
01

Like a class

A class is the plan to make things, not the thing.

Which one is like a class?

02

Start the mold

Which line starts a mold the right way?

03

Only the mold

class Cat: def __init__(self, name): self.name = name

How many cats exist after this code?

04

A real cat

class Cat: def __init__(self, name): self.name = name tom = Cat("Tom") print(tom.name)

What does Python show?

05

Who is self?

Inside the mold, what does self mean?

06

Full battery

class Robot: def __init__(self, name): self.name = name self.battery = 100 zap = Robot("Zap") print(zap.battery)

What does Python show?

07

Fix PyBot's code

PyBot made a robot but forgot its name. The mold needs a name inside the ( ). Make it show: Pip

Goal output
Pip
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A class is a mold. An object is a thing made with it.

class names the mold, __init__ sets up each new robot, and self means “this robot”. Next: many robots from one mold!