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
cls reside only in the method type. If the created method is an instance method then the reserved word
self to 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 to the instance or class methods.
In order 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 instance.
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.
Different Method Types in Python
In Python, there are three different method types: the static method, the class method and the instance method. Each one of them has different characteristics and should be used in different situations.
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 they can be called without instantiating the class. These methods are self-contained, meaning that 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 in order 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.
if number == 0:
return number * MethodTypes.factorial(number - 1)
factorial = MethodTypes.factorial(5)
Class methods have to be created with the decorator
@classmethod, and these methods share a characteristic with the static methods in that they can 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.
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
name = "Ragnar"
# Creates an instance atribute through keyword self
self.lastname = "Lothbrock"
# Access a class atribute through keyword cls
cls.name = "Lagertha"
print("This is a static method")
# Creates an instance of the class
m = MethodTypes()
# Calls instance method