The Secrets of Self
Tags: python, beginners, tutorial, programming
Published: 2022-02-05 15:26:12

in the previous article, we learned 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.

<img src="https://s3.amazonaws.com/revue/items/images/012/597/550/mail/main.py.png?1637788167">

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**. <br>
*What do I mean by that?*

In this picture, we have:

`dog1` : `name = Roxy` & `breed = BorderCollie` <br>
`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. <br>
`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. <br>
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.

<img src="https://s3.amazonaws.com/revue/items/images/012/598/277/mail/main.py__281_29.png?1637791134">

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. <br>
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 <a href="http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html?utm_campaign=xSpace&utm_medium=email&utm_source=Revue%20newsletter">here</a>.


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 <a href="https://twitter.com/_Perceptron_">Twitter</a>. I also have a newsletter, which I send out every week. You can subscribe to it <a href="https://www.getrevue.co/profile/xSpace/">here</a>.