← Python from Scratch
Lesson 61 of 78 · 5 XP
Warm-up: a pet has a name warm-up
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
To give each object its own data, store it in self — "the object itself":
class Pet:
def __init__(self, name):
self.name = name # remembered the name inside the object
cat = Pet("Tom")
print(cat.name) # Tom
__init__ is called automatically when the object is created.
Task. Create a class Pet that remembers the name (name) and age (age) given when it is created: Pet("Rex", 3).
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.