self in Python class


In this tutorial, we are going to learn about the self in Python. You must be familiar with it if you are working with Python. We will see some interesting things about.

Note − self is not a keyword in Python.

Let's start with the most common usage of self in Python.

We'll use self in classes to represent the instance of an object. We can create multiple of a class and each instance will have different values. And self helps us to get those property values within the class instance. Let's see ane example.

Example

# class
class Laptop:
   # init method
   def __init__(self, company, model):
      # self
      self.company = company
      self.model = model

We are defining the properties of a class as self.[something]. So, whenever we create an instance of the class, the self will refer to a different instance from which we are accessing class properties or methods.

Now, let create two instances of the class Laptop and see how the self works.

Example

 Live Demo

# class
class Laptop:
   # init method
   def __init__(self, company, model):
      # self
      self.company = company
      self.model = model
# creating instances for the class Laptop
laptop_one = Laptop('Lenovo', 'ideapad320')
laptop_two = Laptop('Dell', 'inspiron 7000')
# printing the properties of the instances
print(f"Laptop One: {laptop_one.company}")
print(f"Laptop Two: {laptop_two.company}")

Output

If you run the above code, then you will get the following result.

Laptop One: Lenovo
Laptop Two: Dell

We got two different names for the same property. Let's see some details behind it.

Python sends a reference to the instance by default while accessing it methods or And the reference is captured in self. So, for each instance the reference is different. And we will get the respective instance properties.

We know that the self is not a keyword of Python. It's more of like an argument that you don't need to send while accessing any property or method of an instance.

Python will automatically send a reference to the instance for you. We can capture the of the instance with any variable name. Run the following code and see the output.

Example

 Live Demo

import inspect
# class
class Laptop:
   # init method
   def __init__(other_than_self, company, model, colors):
      # self not really
      other_than_self.company = company
      other_than_self.model = model
      other_than_self.colors_available = colors
      # method
      def is_laptop_available(not_self_but_still_self, color):
         # return whether a laptop in specified color is available or not
         return color in not_self_but_still_self.colors_available
         # creating an instance to the class
         laptop = Laptop('Dell', 'inspiron 7000', ['Silver', 'Black'])
# invoking the is_laptop_available method withour color keyword
print("Available") if laptop.is_laptop_available('Silver') else print("Not available")
print("Available") if laptop.is_laptop_available('White') else print("Not available")

Output

If you run the above code, then you will get the following result.

Available
Not available

We have changed the name self to something else. But still, it works as it before. There is no difference. So, self is not a keyword. And moreover, we can change the self to whatever we like to have. It's more like an argument.

Note − best practice is to use the self.. It's a standard that every Python programmer follows

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 11-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements