โ†Previous page

14OBJECTS ยท MANY ROBOTS

One mold, many robots.

Every robot made with the mold keeps its own data. And the mold can give them all the same actions.

IN REAL LIFE

Same mold, different things.

Things from one mold look alike, but each one is its own.

Teddy bears

Same factory mold. You name yours, your friend names theirs.

Team shirts

The same shirt for everyone, but each one has a different name and number.

Cookies again

Same cutter. One gets pink icing, one gets chocolate.

LOOK FIRST

Actions inside the mold.

A method in the mold

def greet(self): inside the class gives every robot a greet action.

self again

When bolt.greet() runs, self is bolt. When rita.greet() runs, self is rita.

Each one its own

bolt.work() lowers only Bolt's battery. Rita keeps hers.

STEP BY STEP

Two robots, one mold.

The mold now has an action. Watch who self is each time.

class Robot:The mold.
def __init__(self, name):Setup: each new robot gets a name.
self.name = nameWrites it inside this robot.
def greet(self):An action for every robot. Its first word is always self.
print("Hi, I am", self.name)Uses this robot's own name.
bolt = Robot("Bolt")Robot number 1.
rita = Robot("Rita")Robot number 2, from the same mold.
bolt.greet()self is bolt. Shows: Hi, I am Bolt
rita.greet()self is rita. Shows: Hi, I am Rita

RUN IT

Put one robot to work.

Guess first. Then run real Python and check.

1

Guess the result

class Robot: def __init__(self, name): self.name = name self.battery = 100 def work(self): self.battery = self.battery - 30 bolt = Robot("Bolt") rita = Robot("Rita") bolt.work() print(bolt.name, bolt.battery) print(rita.name, rita.battery)

What will Python show?

team.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: Add rita.work() two times before the prints. What is Rita's battery now?

YOUR TURN

Whose is it?

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

0Completed
9Not tried
0Review
01

Sprinkles

A cookie cutter made 3 cookies. You put sprinkles on one of them.

How many cookies have sprinkles?

02

Count the dogs

class Dog: def __init__(self, name): self.name = name a = Dog("Rocky") b = Dog("Luna") c = Dog("Max")

How many dogs did the mold make?

03

Hello, Rita

class Robot: def __init__(self, name): self.name = name def greet(self): print("Hi, I am", self.name) rita = Robot("Rita") rita.greet()

What does Python show?

04

Who ate?

class Cat: def __init__(self, name): self.name = name self.hungry = True tom = Cat("Tom") luna = Cat("Luna") tom.hungry = False print(luna.hungry)

What does Python show?

05

Who is self now?

When bolt.greet() runs, who is self?

06

Say hi, Bolt

Which line makes Bolt use its greet action?

07

Fix PyBot's code

Both robots should work once, but PyBot made Bolt work two times. Fix one line. Make it show: Bolt 70 and Rita 70

Goal output
Bolt 70
Rita 70
OUTPUT

08

Fix PyBot's code

PyBot forgot one word in the greet action. Every action in a mold needs self first. Make it show: Hi, I am Pip

Goal output
Hi, I am Pip
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

One mold, many robots, each with its own data.

The class gives every robot the same data names and the same actions. self makes each action use its own robot. You reached the end of the path for now. Great work!