Python Self Variable

'self' is a default variable that contains the memory address of the instance of the current class. So, we can use 'self' to refer to all the instance variables and instance methods. When an instance to the class is created, the instance name contains the memory location of the instance. This memory location is internally passed to 'self'.

1. The 'self' variable is used as first parameter in the constructor as:

def __init__(self):


2. 'self' can be used as first parameter in the instance methods as:

def talk(self):

Here, talk() is instance method as it acts on the instance variables. If this method wants to act on the instance variables, it should know the memory location of the instance variables. That memory location is by default available to the talk() method through 'self'.