How to OOP in Python Like a Pro
Inheritance and composition
If you liked this post, you should support me with a paid subscription. It’s $100 a year, or $10 a month, and in return you get well-researched machine learning/mathematics deep dives such as Machine Learning is Not Just Statistics, Matrices and Graphs, or Vectorization in Theory + Vectorization in Practice.
Your support makes it possible for me to write these high value, high signal posts every week.
Hey!
It’s Tivadar from The Palindrome. Back when I wrote the Mathematics of Machine Learning book, I realized
what a great language Python is,
and that object-oriented Python should be the first thing taught to every machine learning engineer.
So, I’ve been thinking about publishing a series, but my Python skills are not exactly top-of-the-line; I just hack and slash until things work. Fortunately, I found the best person who could do that!
It’s my pleasure to introduce Stephen Gruppetta, author of The Python Coding Stack, and my longtime online friend from back when X was called Twitter.
If you ever wanted to become a power user of Python and take advantage of all the heavy machinery provided by classes, operator overloading, inheritance, composition, etc., this is the article for you.
(It’s the second post in the object-oriented Python miniseries. Check the first one here for the foundations of OOP!)
Dig in!
Cheers,
Tivadar
A class is a template you can use to create several objects that share the same characteristics. All the objects created from a class will have the same structure and can perform the same actions.
However, sometimes you need to create objects that don’t have the same attributes but that are still similar to each other. You may need objects that share some characteristics but are sufficiently different from each other, so you can’t create them using the same class.
You want to avoid writing a brand new class from scratch. Instead, you’d like to reuse some of the code from the original class.
There are several options for handling overlap between classes. Inheritance is a common tool to link classes. However, inheritance is not always the right solution. Composition is an alternative technique to use a class as part of another class. In this article, you’ll learn about inheritance and composition. You’ll learn how to use each technique, and just as importantly, when to use them.
The Velocity Class
You learned about classes in the first article in this series. You wrote a Vector class that you can use to create vector instances.
You’ll make some changes to this class as you progress through this article. However, you’ll primarily work on a new class: the Velocity class.
You’ll consider two different routes to create the Velocity class. Velocity is a vector: it has a magnitude and a direction. Therefore, you can start from the Vector class you defined in the first article and use it as a starting point to build the Velocity class. This is the inheritance route.
However, you can also create a class that has a Vector instance as one of its attributes. You’ll also explore this composition route in this article.




