Python __init__() function | Explained

Python is a versatile language that supports the functionalities of Object-oriented and structured programming. In OOP, the classes and objects play an important role, and all the properties of an object are defined by the class. The object-oriented programming language uses a constructor method to declare the object.

The “__init()__” function is initialized inside the class and plays the same role just like the constructor method in Python. This tutorial will explain the below-listed learning outcomes of the “__init()__” function:

So, let’s get started!

What is __init()__ function in Python?

In Python, the built-in function “__init()__” is called automatically when we create an object. The “__init__()” function allows the class to initialize its object attributes in the argument. This function allows new instances to initialize the attributes with the initial value. The syntax of “__init__()” is shown below:

Basic Syntax

__init__(self, [arguments])
How to install Kali Linux in Window...

The first  “self” parameter is used to represent the instance of the current class. The remaining parameters are utilized to define the attributes of the class.

Let’s head over to the examples of this function

Example 1: __init()__ Function With Parameter Value

In the following code, the “__init__()” function is defined in the class with an argument value. The parameter values of this function are accessed using the object reference. Let’s understand the working of the “__init__()” function via the following code:

Code:

class school:
   
    def __init__(self, name, roll_no, section):
        self.name = name
        self.roll_no = roll_no
        self.section = section

    def info(self):
        print('students information:')
        print('Name         :', self.name)
        print('Roll No    :', self.roll_no)
        print('Section :', self.section)

#create the object of class
school_obj = school('Alex', 18723, 'A')

print(school_obj.name)
print(school_obj.roll_no)
print(school_obj.section)

school_obj.info()

In the above code:

  • The class name “school” is created.
  • The “__init__()” function is initialized inside the class by taking four input argument values. The first argument value is self keywords which indicate an instance of the class. The remaining parameters take the value of class attributes which are initialized as “name”,  “roll_no” and “section”.
  • All attribute values are initialized using the self instance keywords such as “self.name = name
  • Another function named “info” is defined inside the class.
  • Four print statements are used to print the student’s information. (Note: you can define whatever you want inside this function).
  • Lastly, the object named “school_obj” is created for the class.
  • The object is used inside the print() function to call the parameter value of the “__init__()” function.
  • The “info()” function is called directly using the object named “school_obj”.

Output:

In the above snippet, the parameter of the “__init__()” function is accessed using the object of the class.

Example 2: __init__() Function With Inheritance

In the code given below, the “__init__()” function is called inside the child class. Let’s see an example for further understanding:

Code:

class school:
    def __init__(self, name):
        print("Welcome to Python.\n")
        self.name = name
   
class student(school):
    def __init__(self, name, ID):

        school.__init__(self, name)
        print("Learn Python.\n")
        self.name = name
        self.ID = ID
       
#Create object of the school class
obj1 = school("itslinuxfoss")

#Create object of the student class
obj2 = student("Alex", 45)

In the above code:

  • The parent class name named “school” is created.
  • The “__init__()” function is initialized inside the class with two parameters named “self” and “name”.
  • The child class named “students” is initialized in the program, and the name of the parent class is passed inside the parenthesis.
  • The “__init__()” constructor of the child class is defined with three parameters.
  • To access the constructor of the main class, we used the class name along with the “__init__()” function.
  • Print statements define the value which is utilized when the constructor of this class is accessed.
  • Lastly, the object is created for both classes.

Note: The “object” created for the classes is used to access the parameter value, but in our case, we only show the working of the “__init__()” function.

Output:

The above output shows that the first parent constructor calls itself, and the second parent constructor is called in the inherited class. The child constructor is accessed after the parent constructor.

That’s it from this Python guide!

Conclusion

In Python, the “__init__()” function is used to initialize the value of the object attribute of the class. The “__init__()” function also executes the statements that are initialized inside the function when the object is created. The “__init__()‘ constructor accesses the inheritance class at any place inside the function. This article presented all the details regarding the Python “__init__()” function.