Python, just like other object-oriented programming languages, has its main emphasis on objects. Objects represent real-world entities where a class acts as a blueprint. A class consists of variables and functions which act on those variables. We can pass references to each function, such as cls and self. In this article, we shall be comparing python cls vs self.
Different methods in a class
Cls and self are references to pass as arguments to a function in a given class. Before comparing both, we shall first understand the three different types of methods present inside a class:
- Static Method
- Class Method
- Instance Method
Static Method
If we don’t want to use class variables or instance variables, we can use static methods. A static method can be called without the object of the class. We use the decorator @staticmethod to declare a static method. This method requires neither the self nor the cls reference because they are not dependent on any instance or class attribute.
Example:
class Sample:
@staticmethod
def method():
print('This is a static method')
Sample.method()
Output:
This is a static method
Class Method
The class methods are bound to a class instead of the objects of the class. These methods can be called without the instance of the class. We can create class methods by either using the classmethod() method or the decorator @classmethod. Class methods accept cls as an argument indicating that the method points to the class instead of the object instance.
Example:
class Sample:
var = "Class Variable"
@classmethod
def method(cls):
print(cls.var)
Sample.method()
The output is:
Class Variable
Instance method
With the instance method, we can access all the attributes of a class. We pass the self keyword as an argument to the instance method. With the self keyword, we can access the attributes.
In python, every method created inside a class is by default considered as an instance method unless specified explicitly. There is no need for a decorator for this method. With the instance method, we can access the data for each instance of the class.
Example:
class Sample:
def __init__(self, a):
self.a = a
def method(self):
print(self.a)
obj = Sample(10)
obj.method()
Output:
10
What is self in class?
The self keyword is used to pass into the instance methods inside a class. We use the self keyword to represent the instance for a class. Self represents the current instance of the class. Let us consider an example to understand the functionality of the self keyword.
We shall define a class named Person. It consists of the Person’s name and age. With the __init__() method we have initialized the two attributes – name and age. We have also defined a function named details() where we pass self as the function’s argument. Inside the function, we simply print the person’s name and age.
Then, we shall create two objects of the class Person – obj1 and obj2. Here obj1 holds the value name = ‘Liam’ and age = 21. Obj2 holds the value name = ‘Henry’ and age = 25. Then using the objects, we call the details() function.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def details(self):
print(f"Person's name is {self.name} and age is {self.age}")
obj1 = Person('Liam',21)
obj2 = Person('Henry', 25)
obj1.details()
obj2.details()
The output is:
Person's name is Liam and age is 21 Person's name is Henry and age is 25
As we can see here, the self keyword was used to represent the specific instance. Without the self argument, each object would not be able to have a unique value for all the attributes.
The object itself gets passed as the first argument whenever we use the self keyword. Because of this, each object can have its own attributes and methods.
What is cls in class?
Using the cls keyword, we represent that the method belongs to the class. We pass cls as an argument to the class methods. With cls, we attach the class method to the class.
The class methods can be called without having an instance of a class. Using cls, we pass the class as a reference. Let us take an example to understand further.
We shall take the same class Person but add a class method and a class attribute named ‘about’. Before the name of the class method, we shall be using the decorator @classmethod to indicate that the below is a class method. We also have an ‘about’ variable containing a string value for class information.
Here the method info() is a class method. We shall pass the keyword cls as an argument to the info() method. Using cls, we shall print the variable ‘about’.
Then, to call the class method, we shall use the class name.
class Person:
about = 'This class stores the name and age for a person'
def __init__(self, name, age):
self.name = name
self.age = age
def details(self):
print(f"Person's name is {self.name} and age is {self.age}")
@classmethod
def info(cls):
print(cls.about)
Person.info()
The output is:
This class stores the name and age for a person
Python cls vs self
Before comparing cls and self, first we need to understand that neither of them are keywords in python. They are just ideal naming conventions whose name can be changed and yet the functionality would be the same.
cls refers to the class, whereas self refers to the instance. Using the cls keyword, we can only access the members of the class, whereas using the self keyword, we can access both the instance variables and the class attributes.
With cls, we cannot access the instance variables in a class. cls is passed as an argument to the class method, whereas self is passed as an argument to the instance method. If we initialize variables using self, their scope is the instance’s scope. But, variables initialized with cls have the class as their scope.
Must Read
That sums up the comparison between python cls vs self. If you have any questions in mind, leave them below in the comments.
Until next time, Keep Learning!