L4·OOP & Errors
🏛️Classes & Instances
Blueprints for objects with state and behavior.
classself__init__instance
§ 1Anatomy
class Name: body. __init__ initializes each new instance. self is the instance passed automatically.
§ 2Attributes
Instance attributes live on self. Class attributes are shared across instances.
§ 3Methods
Regular methods take self. @classmethod takes cls. @staticmethod takes neither.
Worked example
1class Dog:2 species = "Canis familiaris" # class attr3 def __init__(self, name, age):4 self.name = name5 self.age = age6 def bark(self):7 return f"{self.name} says woof"89fido = Dog("Fido", 3)10print(fido.bark())11print(fido.species)Try it live · Python runs in your browser
Loading playground…
Checkpoint quiz
Question 1 / 2
What does __init__ do?
Lesson test
Take an 8-question test on “Classes & Instances” — concepts, output prediction, bug spotting and interview questions.