โ† Back to Learn
๐Ÿ—๏ธ

Advanced โ€ข Python

Classes & Objects

Lesson 1 of 6
Step 1

๐Ÿ“ What is a Class?

A class is like a blueprint or a cookie cutter. It describes what something should be like. An object is a real thing made from that blueprint, like a cookie made from the cutter. We create an object by writing the class name with brackets, like Dog().

๐Ÿ Python
class Dog:
    sound = "Woof!"

my_dog = Dog()
print(my_dog.sound)
โ–ถ Output
Woof!
โญ Earn 15 XP
๐Ÿงฉ Want to practice? Try the Classes & Objects exercises โ†’

๐Ÿ“š Everything in Classes & Objects

Build your own types of things! Learn to make classes as blueprints, create objects like pets and game characters, give them attributes and methods, and share code with inheritance. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.

  1. ๐Ÿ“ What is a Class?

    A class is like a blueprint or a cookie cutter. It describes what something should be like. An object is a real thing made from that blueprint, like a cookie made from the cutter. We create an object by writing the class name with brackets, like Dog().

    class Dog:
        sound = "Woof!"
    
    my_dog = Dog()
    print(my_dog.sound)
  2. ๐ŸŽฌ The __init__ Method

    __init__ is a special method that runs every time a new object is made. It sets up the object's attributes, which are pieces of information the object remembers. The word self means "this object", so self.name is this object's own name.

    class Pet:
        def __init__(self, name, animal):
            self.name = name
            self.animal = animal
    
    my_pet = Pet("Buddy", "dog")
    print(my_pet.name)
    print(my_pet.animal)
  3. ๐Ÿƒ Methods: Things Objects Can Do

    A method is a function that lives inside a class. Methods are the actions an object can do, like speak or jump. Every method has self as its first parameter so it can use the object's attributes.

    class Pet:
        def __init__(self, name, sound):
            self.name = name
            self.sound = sound
    
        def speak(self):
            print(self.name, "says", self.sound)
    
    cat = Pet("Whiskers", "Meow!")
    cat.speak()
  4. ๐Ÿพ Making Many Objects

    One class can make as many objects as we want! Each object has its own attributes, so every pet can have a different name and sound. We can even put objects in a list and loop through them.

    class Pet:
        def __init__(self, name, sound):
            self.name = name
            self.sound = sound
    
        def speak(self):
            print(self.name, "says", self.sound)
    
    pets = [Pet("Rex", "Woof!"), Pet("Luna", "Meow!"), Pet("Polly", "Squawk!")]
    
    for pet in pets:
        pet.speak()
  5. ๐ŸŽฎ Game Characters

    Objects are perfect for games! A hero can have attributes like name and health, and methods can change those attributes. When the hero takes damage, the method lowers self.health.

    class Hero:
        def __init__(self, name):
            self.name = name
            self.health = 100
    
        def take_damage(self, amount):
            self.health = self.health - amount
            print(self.name, "has", self.health, "health left")
    
    knight = Hero("Sir Kai")
    knight.take_damage(30)
    knight.take_damage(20)
  6. ๐Ÿ‘ช Simple Inheritance

    Inheritance lets a new class borrow everything from another class. We write the parent class in brackets, like class Wizard(Hero). The child class gets all the parent's attributes and methods, and it can add its own new ones too.

    class Hero:
        def __init__(self, name):
            self.name = name
    
        def say_hello(self):
            print("Hi, I am", self.name)
    
    class Wizard(Hero):
        def cast_spell(self):
            print(self.name, "casts a magic spell!")
    
    merlin = Wizard("Merlin")
    merlin.say_hello()
    merlin.cast_spell()