×

python · level 2 · unit 7 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a blueprint, and each is built from it.

A class describes a kind of thing, like a pet. Each object made from it is one real pet with its own details. You have used objects all along: every string is an object of the str class.

Inside the class, __init__ is a method that runs whenever a new object is made. Its first parameter, , is that new object, so self.name = name stores the name as an on it.

Call the class like a function to make an object: Pet("Rex", 4). Read its attributes with a dot, like rex.name.

class Pet:
    def __init__(self, name, age):
        self.name = name
        self.age = age

rex = Pet("Rex", 4)
mog = Pet("Mog", 11)
print(rex.name, rex.age)
print(mog.name)

Class names start with a capital letter, like Pet, so they stand out from variables.