In Python, everything is an object. Even integers are objects — this is different from programming languages like Java where integers, floats, and Booleans are primitive data types. In this way, Python is built on a rigorously consistent object-oriented paradigm.
Considering that objects are everywhere in Python, object-oriented programming is not well studied by Python coders. A huge mistake! Fortunately, you can fix this by example about of Python objects and classes.
If you manage to read over the whole tutorial, you’ll get rewarded with a PDF cheat sheet about classes and objects. So, stay with me! ?♂️
Full Example Code
Here’s a simple example of object orientation in Python. We create two classes
Wizard, and create multiple instances such as
Petunia (Muggles), as well as
Tom (Wizard). Go ahead and skim over the example, I’ll explain it to you in a moment:
class Muggle: def __init__(self, age, name, liking_person): self.age = age self.name = name self.likes = liking_person class Wizard: def __init__(self, age, name): self.age = age self.name = name self.mana = 100 def like_me(self, victim): if self.mana >= 100: victim.likes = self.name self.mana = self.mana - 100 Vernon = Muggle(52, "Vernon", None) Petunia = Muggle(49, "Petunia", Vernon) Vernon.likes = "Petunia" print(Vernon.likes) Wiz = Wizard(42, "Tom") Wiz.like_me(Petunia) Wiz.like_me(Vernon) print(Petunia.likes=="Tom" and Vernon.likes=="Tom")
As you go over this tutorial, you can watch me explain it in the following video.
[Video] Python Classes Objects Example from Harry Potter
Classes Example Explained
You may ask: what is a class anyway?
Classes are “blueprints” for creating objects. A class description tells you everything an object consists of and is able to do (data and functionality).
Today, we are seeing these two concepts in action (with the usual Harry Potter examples):
class Muggle: def __init__(self, age, name, liking_person): self.age = age self.name = name self.likes = liking_person Vernon = Muggle(52, "Vernon", None) Petunia = Muggle(49, "Petunia", Vernon)
From this code snippet, you can learn several things:
- We create a new class “blueprint” for Muggles by using the keyword
class. It is an abstract concept — and it shows how every
Mugglein our microcosm looks like.
- We use the “constructor” method
__init__(...)to initialize the class with data. In our case, every
likes(a person they like). The first value of any class method (including the constructor) is a reference to the object itself. Remember the class describes the functionality and properties of concrete objects. As soon as you call the constructor in your code, Python creates an empty object which you can access using the name
Note that although the first argument is
self when defining a method, you don’t actually specify this argument when calling the method. Python internally does it for you. This is just a piece of syntax that you have to remember.
The constructor is a special method of the
Muggle class: it allows you to initiate a new object by using the name of the class as a function call. For example, the calls
Muggle(52, "Vernon", None) and
Muggle(49, "Petunia", Vernon) creates two new class objects both defining the three variables as follows:
You can see that these objects follow the same blueprint but are different instances of the class
Muggle. The same type of properties but different DNA.
From now on, these objects live in your computer’s memory until Python garbage collects them.
Can you see the tragic element of the story so far? Petunia likes Vernon but he likes nobody. However, as the famous German author “Janosh” said, “if you are loved, you cannot help but giving a little bit of love back”. Let’s make it a happy end, shall we?
Vernon.likes = "Petunia" print(Vernon.likes)
Puzzle 1: What is the output of this code snippet?
Let’s move on. Harry Potter would be boring without wizards. So let’s define the
Wizard class so that we can create some wizards in our small world.
class Wizard: def __init__(self, age, name): self.age = age self.name = name self.mana = 100 def like_me(self, victim): if self.mana >= 100: victim.likes = self.name self.mana = self.mana - 100 Wiz = Wizard(42, "Tom")
Wizard object has three variables:
mana level (how much magic power the wizard has left). On top of that, we add a method called
like_me(...) that is a magic formula the wizard can apply to any victim. If the wizard has enough mana left, he can force the victim to like them.
Tom (aka. Lord Voldemort) is lonely and wants to be liked, so he tries to get Petunia and Vernon to like him:
Wiz.like_me(Petunia) Wiz.like_me(Vernon) print(Petunia.likes=="Tom" and Vernon.likes=="Tom")
Puzzle: Is Tom successful in making both like him?
Make sure that you have completely internalized the syntax of how to create classes and objects in Python. The most common source of confusion is to forget the
self argument when defining a method and that the constructor is defined and called differently:
__init__(...) vs W
Cheat Sheet OOP
To help you dive deeper into the most important features of Python object orientation, feel free to download the following PDF:
Instagram Python OOP Tutorial
For an ultra-short Python object orientation tutorial, check out the following Instagram post (swipe right):
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.