Object-oriented programming with Python
You write organised, reusable Python code using classes, inheritance, special methods and decorators.
Level 201 · 16 lessons · 15 practice labs
Lessons
Class basics
- Why classes? Bringing data and behaviour together: You see why bundling data with its operations beats loose variables and write your first class.
- Creating objects: __init__ and attributes: You build objects with the constructor method and use instance and class attributes in the right place.
- Methods: instance, class and static methods: You learn instance methods using self, and when @classmethod or @staticmethod is the better choice.
- Encapsulation: validated attributes with property: You check values with property whenever an attribute changes, rejecting invalid data inside the object itself.
- Short task: a bank account class that never goes negative
Inheritance and polymorphism
- Inheritance and super(): You derive one class from another, keep shared code in one place and call parent methods with super().
- Polymorphism and composition: inheritance or an object inside an object?: You use one interface across different classes and choose composition where inheritance does not fit.
- Abstract classes: defining a contract with abc: You force subclasses to implement required methods using abc so the structure stays consistent.
- Duck typing and Protocol
- Short task: a shared interface for e-mail and file notifiers
Special methods and tools
- Special methods: __str__, __repr__, __eq__, __len__: You control how your objects print, compare and report length by using Python’s special methods.
- Data classes with dataclass: You write data-only classes briefly with dataclass, using default values and the frozen option.
- Fixed sets of values with Enum
- Decorators: adding behaviour by wrapping a function: You write your own decorator that adds timing or logging to a function without changing its code.
- Short task: a decorator that times and retries a call
Capstone
- Capstone: a small event processor built from log-event classes: You model different log-event types with a class hierarchy and classify them through a single handler.
Practice labs
- Placing an address object inside a customer: Rework a Customer class with scattered address fields so that it holds a separate Address object and show the label output is unchanged.
- Adding properties to a temperature class: Write a class that stores Celsius, computes Fahrenheit with a property and rejects values below absolute zero.
- Writing a classmethod that builds from text: Write a from_line classmethod that builds a staff object from a 'name;email;role' line and a staticmethod that checks the e-mail.
- Keeping a stock item consistent: Stop the quantity going negative and the price being corrupted from outside in the class that tracks Lavanta Otel's minibar stock; the tests probe the rules.
- Fixing a shared class attribute bug: Find the bug where every basket object shares one item list, move the list to an instance attribute and pass the tests.
- Initialising a subclass with super(): Build truck and motorbike classes that inherit from a vehicle class using super(), each computing its load capacity by its own rule.
- Defining an abstract class for payment methods: Define a payment method contract with abc and implement card and transfer classes; the tests check a class missing a method cannot be instantiated.
- Building a storage interface with Protocol: Make a memory-based and a file-based storage class fit one Protocol and get the service working with both without inheritance.
- Replacing inheritance with composition: Rewrite a queue class that misbehaves because it inherits from list using composition, keeping the public behaviour tests passing unchanged.
- Adding special methods to a money class: Add __repr__, __eq__ and __add__ to a class holding an amount and a currency, refusing to add different currencies.
- Writing a sortable record with dataclass: Model a support request as an immutable dataclass that sorts by priority and date, and pass the sorting tests.
- Restricting order states with Enum: Define order states with Enum and write a method that allows only permitted transitions; invalid transitions must raise in the tests.
- Writing a decorator that logs calls: Write a decorator that logs the function name and arguments and keeps the original name and docstring with functools.wraps.
- Writing a decorator that checks roles: Write a @requires_role decorator that raises PermissionError without running the function when the user's role is not enough.
- Writing a cache decorator with expiry: Write a decorator that keeps results per argument set for a fixed time; the tests use a fake clock to check expiry and different arguments.
About this course
You write organised, reusable Python code using classes, inheritance, special methods and decorators.
What you will learn
- Class basics
- Inheritance and polymorphism
- Special methods and tools
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.