Classes for Pythons
Classes are Python's way of bundling data and behaviour into objects. A class defines attributes and methods, __init__ sets up new instances, and inheritance lets one class extend another. Special methods like __repr__ and __eq__ customise behaviour, and protocols and abstract base classes describe what an object can do.
Stage: Object-Oriented Python · Part of the Python roadmap · Last reviewed 19 November 2026
What to learn in Classes
- Define a class with __init__, instance attributes and methods
- Override __repr__ and __eq__ to control printing and comparison
- Extend a base class and call super correctly
- Use dataclasses to avoid repetitive boilerplate
- Explain duck typing and how protocols describe expected behaviour
Classes interview questions
What is the difference between a class attribute and an instance attribute?
A class attribute is defined in the class body and shared by all instances, while an instance attribute is set on self, usually in __init__, and belongs to one object. Mutating a shared mutable class attribute affects every instance.
What is duck typing in Python?
Duck typing means an object's suitability depends on the methods it has, not its class. If it supports the operations the code calls, it works. This is why any object with a read method can be used as a file-like object.
Where Classes fits in the Python roadmap
Classes belongs to the Object-Oriented Python stage (beginner level), which takes about 28 hours. Model problems.
Other units in this stage
Back to the full Python roadmap