Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
self in Python class
In this tutorial, we are going to learn about self in Python classes. The self parameter is a reference to the current instance of a class and is used to access variables and methods belonging to that instance.
Note ? self is not a keyword in Python.
What is self?
We use self in classes to represent the instance of an object. We can create multiple instances of a class and each instance will have different values. The self parameter helps us access those property values within the class instance.
Basic Example
Let's see how self works in a simple class definition ?
# 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}")
The output of the above code is ?
Laptop One: Lenovo Laptop Two: Dell
We got two different names for the same property. This happens because Python sends a reference to the instance by default while accessing its methods or properties. The reference is captured in self. So, for each instance the reference is different, and we get the respective instance properties.
self is Not a Keyword
Since self is not a keyword in Python, it's more like an argument that you don't need to send while accessing any property or method of an instance. Python automatically sends a reference to the instance for you. We can capture the reference with any variable name ?
# 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
print("Available" if laptop.is_laptop_available('Silver') else "Not available")
print("Available" if laptop.is_laptop_available('White') else "Not available")
The output of the above code is ?
Available Not available
We have changed the name self to something else, but it still works as before. There is no difference. So, self is not a keyword and we can change it to whatever we like.
How self Works
When you call a method on an instance, Python automatically passes the instance as the first argument. For example:
class Example:
def show_id(self):
print(f"Instance ID: {id(self)}")
obj1 = Example()
obj2 = Example()
obj1.show_id() # Python calls Example.show_id(obj1)
obj2.show_id() # Python calls Example.show_id(obj2)
print(f"obj1 ID: {id(obj1)}")
print(f"obj2 ID: {id(obj2)}")
The output shows that each instance has a unique ID ?
Instance ID: 140234567890123 Instance ID: 140234567890456 obj1 ID: 140234567890123 obj2 ID: 140234567890456
Note ? Best practice is to use self. It's a standard that every Python programmer follows.
Conclusion
The self parameter is essential for instance methods in Python classes. It refers to the current instance and allows access to instance variables and methods. While you can use any name instead of self, following the convention makes your code more readable and maintainable.
