In Python OOPS programming every one must have come through this
__init__() method. In this Python tutorial, we will learn What is
__init__ method in Python class.
Table Of Contents
In Object-Oriented Programming, we have constructors, which are used to initialize the member variables of the Class, when the class object is created. Constructors are the core concept of Object-Oriented Programming, and are used in other programming languages like Java and C++.
__init__ method is always called when an object is created, and space is allocated to for it. When
__init__ method is called, the calling object is passed as first argument i.e. self. We can use this self parameter to initialize member variables of the object.
__init__ method doesn’t return any value. Also, it is called only once during entire lifetime of an object. If we do not define
__init__ method, then the Python provides default
__init__ method. The parameters in the
__init__ method can also take default values.
See an example of
__init__ method in Python Programming language.
#creating a Class Employee class Employee: # using `__init__` to initialize data for name, age and salary def __init__(self,n='',a=0,s=0): self._name = n self._age = a self._salary = s # display_data() prints the name,age and salary of employee. def display_data(self): print(self._name, self._age, self._salary) e1 = Employee('Nikhil', 22, 25000) e2 = Employee('Nisha', 23, 26000) e1.display_data() e2.display_data()
Nikhil 22 25000 Nisha 23 26000
In the above code and output, we have defined an Employee class with with 3 private members _name, _age and _salary. Also there is one public method display_data() which prints the name, age and salary. The
__init__ method has also been used to initialize the object. The statement
e1 = Employee(), creates an object and its address gets stored in e1. The
self parameter here is like the
this pointer of C++ or
this reference of Java.
__init__ method, the get_data() and set_data() can also be used. But the
__init__ method is a guaranted initialization when the object is getting created.
__init__ and Inheritance
Inheritance is a very important topic in the Object-Oriented Programming, A child class can derive objects and functions from the Parent class. This can be done in Python by just simply passing the First class(parent class) in Second Class(Child class) as an argument and then every property of parent class can be inherited from the Parent class.
See an example code of Inheritance and then we will discuss Inheritance with
class Subject: #initializing objects with __init__ method. def __init__(self,phy,chem,math): self.phy = phy self.chem = chem self.math = math class Student(Subject): # inheriting from class Subject using super().__init__() def __init__(self, phy, chem, math,name): super().__init__(phy, chem, math) self.name = name # creating a function for printing marks and name. def printMarks(self): print('Marks of ',self.name, 'in Phy:', self.phy, ',in chem: ',self.chem, ', in maths: ', self.math) # creating an instace of class Student. s1 = Student(87, 88, 89, 'Nikhil') s1.printMarks()
Marks of Nikhil in Phy: 87 ,in chem: 88 , in maths: 89
In the above code, we have inherited some marks variable from parent class Subject to the class Student, for this we have used super.
In this Python tutorial, we learned about
__init__ method in Python Class. Also we learned about the
__init__() method with the Inheritance which is a very important concept in Object-Oriented Programming.
Make sure to read and write above mentioned code examples, to have the better understanding of the
__init__() method in Python class. Thanks.