Python

Use of the __init__() Function in Python

Python supports both structured and object-oriented programming. The class and object are used to implement object-oriented programming. The class defines the properties of the object. The constructor method is used in object-oriented programming to declare, initialize and manipulate the object, and this method is called automatically when an object of the class is created. The __init__() function works like the constructor method in Python and it is declared inside the class.

Syntax:

The syntax of the __init__() function is given below.

__init__(self, [arguments])

The first argument of the function is used to refer to the current object of the class. The other arguments of this function are optional. Zero or more arguments can be used after the first argument to initialize the class variables. Different uses of the __init__() function have been in the next part of the tutorial.

Example-1: Use of a Constructor Without Any Argument

Create a Python file with the following script to define a constructor method without any argument by using __init__() function. This function will be called when the object of the TestClass will be created.

#Declare a class with parameterless constructor

class TestClass:

#Declare constructor method

def __init__(self):

#Print a simple message

print('Constructor method has been called.')

#Create object of the class

object = TestClass()

Output:

The following output will appear after executing the above script. The __init__() function has been called when the object of the class was created, and the message from the __init__() function, ‘Constructor method has been called.’ has been printed.

Example-2: Use of a Constructor with Arguments

Create a Python file with the following script to define a constructor method with an argument by using the __init__() function. Two objects have been created for the TestClass class in the script. So, the __init__() function will be called two times and initialize the name variable with the value passed to the function at the time of the object creation.

#Declare a class with a parameterized constructor

class TestClass:

#Declare constructor method with one argument

def __init__(self, name):

#Print a simple message

print('Constructor method has been called.')

#Initialized the class variable

self.name = name

#Create object of the class

object1 = TestClass('Mir Abbas')

print('Welcome, ', object1.name)

#Create another object of the class

object2 = TestClass('Nila Chowdhury')

print('Welcome, ', object1.name)

print('Welcome, ', object2.name)

Output:

The following output will appear after executing the above script. The first object has been created with the value, ‘Mir Abbas’, and the second object has been created with the value, ‘Nila Chowdhury’. The output shows that the second object creates a clone of the first object. So, the property value of the first object didn’t overwrite by the second object.

Example-3: Use of a Constructor with Another Method

Create a Python file with the following script where a constructor method has been declared with the other method. Here, the __init__() function will take two argument values at the time of the object creation that will be used to initialize two class variables, and another method of the class will be called to print the values of the class variables.

#Declare a class with parameterized constructor

class TestClass:

#Declare constructor method with one argument

def __init__(self, name, profession):

#Print a simple message

print('Constructor method has been called.')

#Initialized the class variables

self.name = name

self.profession = profession

#Call another method

self.display_info()

#Define another method of the class

def display_info(self):

print("The profession of ", self.name, " is ", self.profession)

#Create object of the class

object = TestClass('Kabir Hossain', 'CEO')

Output:

The following output will appear after executing the above script. Two class variables have been initialized with the values, ‘Kabir Hossain’ and ‘CEO’ at the time of the object creation and these values have been printed.

Example-4: Use of a Constructor with Inheritance

Create a Python file with the following script where a constructor method has been implemented with the feature of inheritance. The __init__() function has been defined for both the parent class and child class here. The display() method has been defined also for both the parent and child classes. The __init__() function of the parent class has one argument and the child class has three arguments.

#Declare the parent class

class ParentClass:

def __init__(self, name):

print("The parent constructor has been called.\n")

self.name = name

def display(self):

print("Name: ", self.name)

#Declare the child class

class ChildClass(ParentClass):

def __init__(self, name, post, salary):

#Call constructor of the parent class

ParentClass.__init__(self, name)

print("The child constructor has been called.\n")

self.post = post

self.salary = salary

def display(self):

print("Name: ", self.name)

print("Post: ", self.post)

print("Salary: ", self.salary)

#Create object of the parent class

object1 = ParentClass("Tanvir Hossain")

object1.display()

#Create object of the child class

object2 = ChildClass("Farheen Hasan", 'CEO', 700000)

object2.display()

Output:

The following output will appear after executing the above script.

Example-5: Use of a Constructor with Multiple Inheritance

Create a Python file with the following script where the child class has been created from two parent classes and the __init__() function has been defined for these three classes. The child class has another method named display(), to print the values of the class variables.

#Declare the parent class

class ParentClass1:

def __init__(self, name, email, contact_no):

print("The parent constructor has been called.")

self.name = name

self.email = email

self.contact_no = contact_no

#Declare the parent class

class ParentClass2:

def __init__(self, department, post):

print("Another parent constructor has been called.")

self.department = department

self.post = post

#Declare the child class

class ChildClass(ParentClass1, ParentClass2):

def __init__(self, name, email, contact_no, department, post, salary):

#Call constructor of the parent class

ParentClass1.__init__(self, name, email, contact_no)

# Call constructor of another parent class

ParentClass2.__init__(self, department, post)

print("The child constructor has been called.\n")

self.salary = salary

def display(self):

print("Name: ", self.name)

print("Email: ", self.email)

print("Contact No: ", self.contact_no)

print("Department: ", self.department)

print("Post: ", self.post)

print("Salary: ", self.salary)

#Create object of the child class

object = ChildClass('Farhan Akter', '[email protected]', '8801937894567', 'HR', 'Manager', 500000)

#Call the display method

object.display()

Output:


The following output will appear after executing the above script.

Conclusion

The ways of using the __init__() function in Python have been shown in this tutorial by using simple examples for helping the Python users to know the purposes of using this function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.