Chapter11 Lessons

OOP

Object-Oriented Programming concepts and patterns. Explore class mechanics, inheritance hierarchies, method resolution order (MRO), encapsulation, and custom iterators.

Chapter Lessons

01

Basic Class

A class is made up of methods and state. This allows code and data to be combined as one logical entity. This module defines a basic car class, creates a car instance and uses it for demonstration purposes.

02

Dataclass

Python 3.7

Data classes (PEP 557), introduced in Python 3.7, provide a declarative syntax for classes that primarily store state.

03

Inheritance

Inheritance is a way to reuse code and data from a parent class. This allows us to avoid repeating ourselves and to build upon existing functionality. This module defines a basic vehicle class, creates a car class that inherits from vehicle, then creates a truck class that inherits from car and use it for demonstration purposes.

04

Encapsulation

Encapsulation is a feature of OOP that allows you to hide the implementation details of a class from its users. Encapsulation allows us to limit the access of certain attributes within a class. This prevents users from directly accessing and modifying such attributes from outside the class. Instead, users must use methods to access and modify attributes.

05

Property

Properties provide a Pythonic mechanism for managed attributes.

06

Enum

Enumerations (`enum` module) provide a way to define sets of symbolic names bound to unique, constant values.

07

Abstract Class

Abstract class is an extension of a basic class. Like a basic class, an abstract class has methods and state. Unlike a basic class, it inherits the `ABC` class and has at least one `abstractmethod`. That means we cannot create an instance directly from its constructor. In this module, we will create an abstract class and two concrete classes.

08

Exception Class

Exception classes are used to indicate that something has gone wrong with the program at runtime. Functions use the `raise` keyword, if an error is anticipated, and specify the exception class they intend to throw. This module defines a handful of custom exception classes and shows how they can be used in the context of a function.

09

Iterator Class

Iterator classes implement the `__iter__` and `__next__` magic methods. This module defines an employee iterator class that iterates through each employee in a hierarchy one-by-one. This module also shows how a similar approach can be achieved with a generator function.

10

Mixin

Mixins are a way to provide implementation details that simplify the work for a developer to conform to an opaque interface specification. In this module, we have one request handler interface and two request handler mixins to illustrate how mixins can make our lives easier when defining concrete classes.

11

Mro

MRO stands for method resolution order, and it's used by class definitions to determine which method will be run by a class instance. This module shows how the MRO is useful for the classic diamond problem where classes B and C depend on class A, and class D depends on classes B and C.