Constructor Instance Variable and Self in Python
In this class, we discuss Constructor Instance Variable and Self in Python.
For Complete YouTube Video: Click Here
Constructor Instance Variable
The reader should have prior knowledge of the class, object, and memory allocation to the object. Click here.
Have a quick recap of the concepts of class and object.
In our last class, we have taken a google example to understand the class and object.
In the class google, we had an input variable. And a search method.
Whenever we create an object, a separate memory is allocated to each object.
Example:
x=google(“India”) we created an object of class google, and the input string is India.
A separate memory is allocated to object x. Assume the memory allocated at location 50.
In the allocated memory, input is stored. In our example, India is stored.
Take one more object, y= google(“japan”). the object y is assigned a new memory, and input japan is stored in the memory.
This basic understanding of an object is discussed in our previous classes.
One more definition to object. The object is an instance of a class.
Whenever an object is defined, a separate instance is created. I.e., a distinct memory is created.
Instance Variable
The variables that are stored in the memory allocated for the object are called instance variables.
In our above example, the input string is an instance variable.
Take a sample and understand how to define constructor and instance variable.
class Test:
def __init__(self,k):
self.instvar=k
def f(self):
print(self.instvar)
def f1(self):
M=40
print(self.instvar+M)
x=Test(40)
y=Test(80)
x.f()
In the above program, we had a class called a test.
In that class, we had a __init__ method. And in the method we have parameters self and parameter k.
We discuss the parameter self later in this session.
We had another two methods named f and f1.
Whenever we create an object. Python interpreter will automatically call init method.
Example:
x=Test(40) we defined an object.
Whenever we define an object, a separate memory is allocated. Assume memory assigned at location 50.
The memory diagram is shown below.
self
After defining the object, separate memory is allocated, and the python default call init method.
The init method in the above example has a variable self. The self variable is assigned the reference of the object.
In our example, variable self-referencing to memory location 50. This allocation is done automatically by a python interpreter.
The first variable in the method init is self. If we want to define another parameter, we define it after parameter self.
For any method in the Python class, the first variable should be self.
Python, by default, assigns the object reference to the self variable.
In our example, when we defined the object. We use a value of 40.
The value 40 is assigned to parameter k. Not to self. The first parameter, self, is an object reference.
Constructor
The init method defined in the class is called the constructor method.
Python, by default, calls the constructor method when an object is created.
In the body of the constructor method, we defined self.instvar=k.
self. instvar says that create a variable instvar in object memory.
In object x, we had a variable instvar, and this variable is referencing to value 40.
The variables present in the object we call them instance variables.
In our example, instvar is an instance variable.
The variable k in the init method is the local variable. We can use the variable k in the method.
The concept of a local and global variable is discussed previously. Click here.
Note: Variable instvar is a global variable to this class. Any method in the class can use the variable instvar.
Instance variables can be used by any method in the class.
In the program above, one more object is created. y=Test(80).
A new memory is assigned to this new object. And the variable instvar is given value 80.
Assume the memory allocated to the object y is location 80.
Whenever we call x.f(). we are calling method f using object x.
Object x is referencing memory location 50. so self variable is assigned memory location 50.
The method f is displaying self.instvar. Self is pointing to memory 50. so in Memory 50, variable instvar is having a value of 40.
The output displayed is 40.
Point to understand
Each object has its own instance variables.
Take one more example for a better understanding of the concept.
class Test:
def f(self):
self.instvar=20
def f1(self):
print(self.instvar)
x=Test()
x.f()
In the above program, we do not have a constructor method.
We have methods f and f1.
In method f, we are creating an instance variable and assigned value 20.
Point to understand
We can define instance variables not only in the constructor but in any method.
The reason to define instance variables in the constructor method?
The constructor method is called by default during object creation. The remaining methods are not called by default.