Have you ever wondered why some methods in Python receive the self keyword as a parameter, others cls and others nothing at all?
In simple terms, it comes down to method types. self is used to access an instance method, cls is used to access a class method and nothing is applied when it’s a static method.
Difference Between Cls and Self Python Methods
clsis a keyword that’s used to access a class method, which is a code template for creating objects.selfis a keyword that’s used to access an instance method, which retrieves all the attributes of a class.
Cls vs. Self Explained
The difference between the keywords selfand cls resides only in the method type. If the created method is an instance method, then the reserved word self must be used. But if the method is a class method, then the keyword cls must be used. Finally, if the method is a static method, then none of those words will be used. Static methods are self-contained and don’t have access to the instance or class variables, nor the instance or class methods.
To understand these keywords better and why Python asks the programmer to send them as a parameter, I will go over the difference between a class and an instance.
Class vs. Instance in Python
To further understand how self and cls work in Python, it helps to know how classes and instances are related to each other.
What Is a Class in Python?
A class is a code template for creating objects (like a blueprint to build a house). A class defines variables and all the different capabilities associated with the described object. In Python, a class is created by the keyword class.
What Is an Instance in Python?
An instance is an object created using the constructor of the class. In other words, an instance is a particular copy of a class.
Different Method Types in Python
In Python, there are three different method types: the static method, the class method and the instance method. Each of them has different characteristics and should be used in different situations.
Static Methods
A static method in Python must be created by decorating it with @staticmethod. This lets Python know that the method should be static. The main characteristic of a static method is that it can be called without instantiating the class. These methods are self-contained, meaning they can’t access any other attribute or call any other method within that class.
You could use a static method when you have a class, but you don’t need a specific instance to access that method. For example, if you have a class called Math and you have a method called factorial, you probably won’t need a specific instance to call that method. So, you could use a static method.
class Math:
@staticmethod
def factorial(number):
if number == 0:
return 1
else:
return number * MethodTypes.factorial(number - 1)
factorial = MethodTypes.factorial(5)
print(factorial)
Class Method
Class methods have to be created with the decorator @classmethod, and these methods can also be called without having an instance of the class. The difference relies on the capability to access other methods and class attributes but no instance attributes.
Instance Methods
This method can only be called if the class has been instantiated. Once an object of that class has been created, the instance method can be called and can access all the attributes of that class through the reserved word self. An instance method is capable of creating, getting and setting new instance attributes and calling other instance, class and static methods.
Example of Different Method Types
class MethodTypes:
name = "Ragnar"
def instanceMethod(self):
# Creates an instance atribute through keyword self
self.lastname = "Lothbrock"
print(self.name)
print(self.lastname)
@classmethod
def classMethod(cls):
# Access a class atribute through keyword cls
cls.name = "Lagertha"
print(cls.name)
@staticmethod
def staticMethod():
print("This is a static method")
# Creates an instance of the class
m = MethodTypes()
# Calls instance method
m.instanceMethod()
MethodTypes.classMethod()
MethodTypes.staticMethod()
Frequently Asked Questions
What is cls in Python?
Cls is a naming convention for accessing a class method in Python. A class describes the variables and capabilities of an object, essentially acting as a blueprint for creating an object.
What is the difference between self and cls?
The main difference between self and cls is the types of methods they denote. Class methods use cls while instance methods use self. A class acts as the blueprint for building an object, and an instance is a specific copy of that object.
When to use self for variables?
Whenever an object is created from a class, self is used to refer to that particular instance of the object. As a result, self should be used whenever an instance method is involved.
What happens if we don’t use self in Python?
If you either avoid using self or replace it with a different name, this can cause an error to occur. Other programmers will also have a harder time reading your code, potentially leading to more issues down the road.
