Lesson 23 / 47

Classes, Objects & __init__

A class is a blueprint; an object is one instance built from it.

__init__ and self

class defines the blueprint. __init__ runs automatically when you create an object, and self refers to that specific object.

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

    def bark(self):
        return f"{self.name} says Woof!"

rex = Dog("Rex", 3)
print(rex.bark())   # Rex says Woof!

Blueprint vs instance

Think of a class as a cookie cutter and objects as the cookies — same shape, different dough (data) each time.

Why self?

self is just a convention name for the first parameter — Python passes the instance automatically; you never pass it yourself when calling rex.bark().