The Secrets of Self

Understanding the working of `self` in Python Classes

The Secrets of Self

In the previous article, we spoke about how Object-Oriented Programming can be replicated multiple times or can be reused later for fulfilling different utilities. For example, we can import a library (a well-documented pre-written code) into our program and create objects from there to use them inside our code.

While we were learning about Object-Oriented Programming, we saw the repetitive pattern for a parameter inside the Class in general as self. Let’s dive a bit into understanding, how self works and why do we use it?

Why do we need self?

Let’s take a look at the Class Animal. We have explicitly defined self while introducing attributes to the class as well as while we are defining a method.

We by now know that a Class creates a blueprint for us to create an Object. In the above picture, we have created two Objects named dog1 and dog2. The self term helps us represent the respective instance of an Object.
What do I mean by that?

In this picture, we have:

dog1 : name = Roxy & breed = BorderCollie
dog2 : name = Brent & breed = Doberman

Both the Objects are created from the same class i.e. from the same blueprint, the self parameter helps identify each object we have created and assign respective attributes and methods to it.
dog1 holds the name Roxy and breed as BorderCollie whereas dog2 holds the name Brent and the breed as Doberman. If we didn’t have the self parameter these would have not been possible.

How self works?

I mentioned a hint of how self works in the previous article but let’s dive into understanding what exactly happens under the hood.

For anyone, looking at the method breathe() would seem really weird that it takes one argument however when we call it, we don’t actually need to pass an argument for it to work nor does Python tell us anything about it.

When we run the above commands in the file type.py we see that Animal.breathe is a function while dog1.breathe is a method and that is quite interesting. Why? Well, the thing with Python method is that it passes the object as the first parameter inside the function.

So, when I type in dog1.breathe() into my console, it internally passes Animal.breathe(dog1) as a function with the self parament being the object we create itself.

Conclusion

Before we end, a quick notion that the self term doesn’t really need to be self, you can put it as dinosaur and it would still work the same, just make sure you replace all of them with dinosaur.

However, it’s generally recommended that you use self, as that’s followed by the entire community. Additionally, if you are interested you can read more on the self parameter from the creator of Python itself here.

I hope I was able to explain why we need self and how it works under the hood. If you have any interesting suggestions or feedback, feel free to connect with me on Twitter. I also have a newsletter, which I send out every week. You can subscribe to it here.