In Python, everything is an object. Even integer values 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 comparably little known by Python coders. A huge mistake! Fortunately, you can fix this by reading this article from my “Coffee Break Python” email series:
Join us, it’s fun!
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:
1) We create a new class “blueprint” for Muggles by using the keyword “class”. It is an abstract concept — and it shows how every Muggle in our microcosm looks like.
2) We use the “constructor” method __init__(…) to initialize the class with data. In our case, every Muggle has variables “age”, “name”, and “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 “self”.
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:
| Muggle |
| age = 52 |
| name = "Vernon" |
| likes = None |
| Muggle |
| age = 49 |
| name = "Petunia" |
| likes = "Vernon" |
You can see that these objects follow the same blueprint but are different instances of muggles. The same type of properties but different DNA.
From now on, these objects live in your computer’s memory until Python kills them (more on this in a later email).
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")
Every Wizard object has three variables: age, name and 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 on any victim. If the wizard has enough mana left, he can force the victim to like them.
Tom 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 Wizzard(…).
Puzzle 1: “Petunia”
Puzzle 2: False