ThePythonBook

Object-Oriented Programming

Classes, inheritance, magic methods, dataclasses, and design patterns

13tutorials
76exercises
315minutes
1155XP
13 tutorials in this category

Python Classes and Objects: OOP from the Ground Up

Build your first class: __init__, self, methods, attributes, and __str__. The foundation of object-oriented Python.

intermediate30m795

Python Inheritance: Subclasses, Polymorphism, and super()

Subclasses, method overriding, super(), and polymorphism — reuse and extend code without rewriting it.

intermediate25m685

Python @classmethod vs @staticmethod: When to Use Each

@classmethod builds alternative constructors; @staticmethod groups utility functions. Know when to reach for each.

intermediate20m570

Python @property: Getters, Setters, and Computed Attributes

Turn method calls into attribute access with @property. Add validation, compute values on the fly, and keep your API clean.

intermediate20m690

Python Magic Methods: The Complete __dunder__ Guide

The __dunder__ methods that make Python objects behave like built-in types — __str__, __len__, __eq__, __getitem__, and more.

advanced30m8120

Python Operator Overloading: Make Classes Work with +, -, ==

Make your classes work with +, -, ==, and other operators using __add__, __sub__, __eq__, and their reflected variants.

advanced25m695

Python Abstract Base Classes: Enforce Interfaces with ABC

Enforce interfaces in Python with Abstract Base Classes. Define contracts that subclasses must follow.

advanced25m585

Python Multiple Inheritance and MRO: How It Actually Works

How Python resolves the diamond problem with MRO, how super() works in complex hierarchies, and practical mixin patterns.

advanced25m595

Python Dataclasses: Less Boilerplate, More Readable Code

Cut the boilerplate. @dataclass generates __init__, __repr__, and __eq__ for you. Less typing, more readable data classes.

intermediate25m7105

NamedTuple vs TypedDict vs Dataclass: Which to Choose?

Three ways to structure data — NamedTuple, TypedDict, and dataclass. When each one fits and when it doesn't.

intermediate20m570

Python __slots__: Reduce Memory Usage by 40%

Reduce memory usage by ~40% with __slots__. How it works, when to use it, and what you give up.

advanced20m575

Advanced Dataclasses: __post_init__, frozen, field(), slots, and Inheritance

Validation with __post_init__, immutability with frozen=True, complex defaults with field(), and dataclass inheritance.

advanced25m690

Composition vs Inheritance: Why \

Why 'has-a' often beats 'is-a'. Refactor fragile inheritance into clean composition with dependency injection.

advanced25m580