Cls vs. Self: Understanding Method Types in Python

Understand the difference between the keywords self and cls in Python and when to use each method type. 

Published on Oct. 17, 2023
software developer writing python code on multiple monitor computer
Image: Shutterstock / Built In
Brand Studio Logo

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

  • cls is a keyword that’s used to access a class method, which is a code template for creating objects.
  • self is 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 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.

More on Python6 Important Things to Know About Python Functions

 

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.

A tutorial on class, static and instance methods in Python. | Video: Indently

More on PythonPython Auto formatter: Autopep8 vs. Black

 

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.

 

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 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.

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 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.

 

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()
Explore Job Matches.