Demystifying Python’s ‘self’ and ‘cls’: Understanding Usage and Differences with Examples

When working with Python’s object-oriented programming (OOP) features, you’ll often encounter the terms ‘self‘ and ‘cls‘. These two keywords play crucial roles in defining and manipulating class attributes and methods. In this article, we will delve into the usage and differences between ‘self‘ and ‘cls‘ in Python, providing clear examples to illustrate their functionality.

1. Understanding ‘self’.

  1. In Python, ‘self‘ is a special keyword that refers to the instance of a class.
  2. It is the first parameter passed to all instance methods, allowing you to access and modify instance-specific attributes and invoke other instance methods.
  3. Let’s explore its usage with an example:
    class Dog:
        def __init__(self, name, age):
   = name
            self.age = age
        def bark(self):
            print(f"{} says Woof!")
    # Creating instances of the Dog class
    dog1 = Dog("Buddy", 3)
    dog2 = Dog("Max", 2)
    # Accessing instance attributes
    print(f"{} is {dog1.age} years old.")
    print(f"{} is {dog2.age} years old.")
    # Calling instance method
  4. In this example, ‘self‘ allows us to access the ‘name‘ and ‘age‘ attributes unique to each instance of the ‘Dog‘ class.

2. Understanding ‘cls’.

  1. On the other hand, ‘cls‘ is another keyword in Python, which stands for “class“.
  2. It is used to access and modify class-level attributes and methods.
  3. cls‘ is typically used within class methods, which are methods that are bound to the class rather than an instance.
  4. Here’s an example to illustrate its usage:
    class Circle:
        pi = 3.14
        def __init__(self, radius):
            self.radius = radius
        def circumference(cls, radius):
            return 2 * cls.pi * radius
    # Creating instances of the Circle class
    circle1 = Circle(5)
    circle2 = Circle(7)
    # Accessing class-level attribute
    print(f"Value of pi: {Circle.pi}")
    # Calling class method using 'cls'
    circumference1 = Circle.circumference(circle1.radius)
    circumference2 = Circle.circumference(circle2.radius)
    print(f"Circumference of circle1: {circumference1}")
    print(f"Circumference of circle2: {circumference2}")
  5. In this example, ‘cls‘ is used to access the ‘pi‘ attribute, which is shared by all instances of the ‘Circle‘ class. We use a class method ‘circumference‘ to calculate the circumference of different circles, with ‘cls.pi‘ representing the value of pi for all instances.

3. Differences between ‘self’ and ‘cls’.

  1. self‘ is used to refer to instance-specific attributes and methods, while ‘cls‘ is used to access class-level attributes and methods.
  2. self‘ is always the first parameter in instance methods, while ‘cls‘ is the first parameter in class methods.
  3. self‘ is used within instance methods, and its value is automatically provided by Python when calling the method. ‘cls‘ is used within class methods, and you need to pass it explicitly when calling the method.
  4. self‘ is used to modify and access instance attributes, while ‘cls‘ is used to modify and access class attributes.

4. Conclusion.

  1. In Python, ‘self‘ and ‘cls‘ are essential keywords used in object-oriented programming to work with instance-specific and class-level attributes and methods, respectively.
  2. Understanding their usage and differences is crucial for effective OOP in Python.
  3. With the examples provided in this article, you should now have a clearer grasp of how ‘self‘ and ‘cls‘ function in Python classes, enabling you to use them effectively in your own projects.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.