What Is The Self Keyword In Python?

What Is The Self Keyword In Python?

Published on October 01 2023

In this tutorial, we will learn about the self keyword in Python and its usage. This tutorial is part of Python's top 25 most commonly asked interview questions.

Definition

  • Self represents an object or an instance of a class.
  • In Python, this is explicitly included as the first parameter in the constructor and instance method.
  • It helps to differentiate between the instance attributes ( and methods ) from local variables.
  • often thought to be a keyword but self is not a keyword in Python.
  • It can be named anything other than “self” but recommended to use self.

Sample Code

class A: 
    def func(self): 
        print("Hello World!")

To know about it, please refer to the official documentation website - official website.