📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero OOP — Classes

OOP — Classes

6 min read Quiz at the end
Define classes with class and create objects with MyClass(). __init__ initialises the object. self refers to the current instance. Instance attributes are unique per object while class attributes are shared.

Object-Oriented Programming

class Animal:
    def __init__(self, name, sound):
        self.name  = name
        self.sound = sound

    def speak(self):
        return f"{self.name} says {self.sound}"

dog = Animal("Rex", "Woof")
print(dog.speak())   # Rex says Woof
print(dog.name)      # Rex
Topic Quiz · 5 questions

Test your understanding before moving on

1. What is __init__?
💡 __init__ is the initialiser (constructor) — called when an object is created.
2. What does self refer to?
💡 self refers to the current instance of the class.
3. How do you create an object?
💡 Objects are created by calling the class like a function: obj = ClassName(args).
4. What is a class attribute?
💡 Class attributes are defined on the class and shared by all instances.
5. What does instance attribute mean?
💡 Instance attributes (self.attr = value) are unique to each object.