Self and InIt Method in Python
In this blog will discuss about why do we need init method in python.
Init Method:-
Init method is used to initialize the parameters or arguments of the object which is created from the class.
This method takes the object as its first argument (self), followed by any additional arguments that need to be passed to it.
The __init__ function is called every time an object is created from a class.
Syntax:-
class A:
def __init__(self):
passBy default __init__ method contains self inside it, it refers to the current object of the class. Let us understand this by the following example.
Look into the Example code below to define two objects with our class
class A:
pass
obj1=A()
obj1.name="Praveen"
obj1.gender="M"
obj1.mobile_number = "+91-987654321"
obj2=A()
obj2.name="Aparna"
obj2.gender="F"
obj2.mobile_number = "+91-9123456789"
print("The name of second object is ", obj2.name)
Let us write the same code using init method and self in class.
class A:
def __init__(self,name,gender,mobile_number):
self.name=name
self.gender=gender
self.mobile_number=mobile_number
obj1=A("Praveen","M","+91-987654321")
obj2=A("Aparna","F","+91-9123456789")
print("Name of the Second Object is ",obj2.name)Understanding the code:-
we define a class as “class A” and we defined init method inside of it. It takes name,gender and mobile number as the arguments.
Get Praveen Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
So when we create a object from this class A, we need to pass those init method parameters. It will send a error if we didn’t pass those arguments.
Self:- In the init method self refers to the specific to the object.
we created a “obj1” from class A, it is having parameters as
name=Praveen
gender=M
mobile_number=+91–8317563440
so self refers to obj1 variables and methods. we can access the method variable with “obj1.name”.
In this way we can access the attributes inside the class
Now we will see how we can access the method inside the class.
Understand this using the below code.
class A:
def __init__(self,name,gender,mobile_number):
self.name=name
self.gender=gender
self.mobile_number=mobile_number
def printing_name(self):
print("The Name Of the Object is ", self.name)
def printing_gender(self):
print("Gender is ",self.gender)
def printing_mobile_number(self):
print("Mobile Number is ", self.mobile_number)
obj1=A("Praveen","M","+91-987654321")
obj2=A("Aparna","F","+91-9123456789")
print("Accesing the 1st Object Arguments and Methods from Class A")
obj1.printing_name()
obj1.printing_gender()
obj1.printing_mobile_number()
print("--------------------------------------------------")
print("Accesing the 2nd Object Arguments and Methods from Class A")
obj2.printing_name()
obj2.printing_gender()
obj2.printing_mobile_number()As you can see in the above code, we can access the method of the class using the “object name.method name”.
Remember :- __init__ is responsible for initializing the attributes of the newly created object.








