Introduction to Object-Oriented Programming in Python
The true way of doing stuff with data
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, and even better, he agreed to write a special article for you!
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.
Dig in!
Cheers,
Tivadar
“A computer program stores data and does stuff with the data.”
This is not the most technical definition of a computer program you’ll see. But it’s a valid one. When you learn to code, you learn about data structures to store different types of data. And you also learn about tools needed to manipulate and transform the data. You often define functions containing code to “do stuff” with the data.
Your code will contain data structures and functions, and you pass those data structures to the functions when needed.
Object-oriented programming (OOP) brings these two aspects together into a single unit. This unit is the object, which contains the data and the tools needed to manipulate the data. This may not sound like much, but it enables you to think about the problem you’re trying to solve differently. You can visualize your problem in a way closer to how humans see the world.
Let’s look at some examples, starting with a concrete one. Consider a country. There’s plenty of data relevant to a country: its name, population size, geographical area, capital city, and more. All countries have these attributes. Therefore, you can create a template that includes these attributes that you can use each time you want to represent a different country. Countries also include people, so these can be included in the data for each country, too.
But countries also perform actions. They issue passports, they collect taxes, they create laws, and so on. OOP urges you to think of a single unit to represent a country, which includes all the data and tools needed for the country to perform the actions required. You’d create a class called Country–this is the template you need to create lots of countries. The class doesn’t represent a specific country but the idea of a country. Once you define the class, you can create as many instances of the class as you need. Each instance represents a specific country.
Classes and Instances • The Vector Class
But let’s work on a different example in this article. Let’s consider a vector. One way to view vectors is as entities with both magnitude and direction. In three-dimensional (3D) Euclidean geometry, a vector is represented by three numbers.
Let’s put on our OOP hat. We need a unit in our program to represent a vector. It needs to represent its data and its functionality. Let’s start by creating a class called Vector:
Admittedly, this class doesn’t do much for now. The ellipsis (...) is just a placeholder that’s valid Python syntax. You’ll add more to this class soon. In the previous section, I mentioned that a class is a template for creating many objects modelled from the same blueprint. Let’s create a few instances of this class:
There’s only one Vector class. But now you have two instances of this class. You create an instance of the class when you add parentheses after the class name. The objects referenced by v1 and v2 are separate objects that occupy different areas of your computer’s memory. You can confirm that they’re different objects by showing their identity using Python’s id() function. The two instances of the Vector class have different identities:
You’ll get different values from the ones shown here when you run this code on your computer. But what matters is that the two numbers you get are different from each other. You can also confirm that v1 and v2 represent different objects by using v1 is v2, which returns False.
Note that the terms object and instance are both commonly used to refer to the unit created by a class. They refer to the same thing.
However, these are “blank” objects. They don’t have anything beyond the bare minimum a Python object needs. Let’s add some data.







