What is Python __init__ method [With Examples]

In this Python tutorial, we will discuss the __init__ method in Python. We will have an overview of the following topics of Python __init__ method, which are as follows.

  • What is __init__ in Python?
  • The syntax used for __init__.
  • Execution of __init__ class in Python.
  • Inheritance in __init__ method.

What is __init__ in Python?

In Python __init__ method is used to define a constructor of a class method. A constructor method contains a group of statements that are carried out during the formation of an object.

As soon as a class object is created, it is executed. Any initialization you want to perform on your object can be done with the __init__ method.

The syntax used for __init__

Python declares the __init__ method inside a class using the following syntax.

class ClassName:
         def __init__(self, 'arguments like name, role, etc' ):
             # Required Initialisations
     
        # Other member functions
                ……
               …….

Execution of __init__ class in Python

We start defining a class with a class keyword and add the colon. Also, any code below this class syntax is part of that class only. To explain this __init__ function let’s take an example class.

class Employee:
    def __init__(self,name,role):
        self.name = name
        self.age = age

The properties that all Employee objects must have been defined in a method called .__init__(). Every time a new Employee object is created .__init__() sets the initial state of the object by assigning the values of the object’s properties. That is, .__init__() initializes each new instance of the class.

class Employee:
    def __init__(self,name,role):
        self.name = name
        self.role = role
    def emp_details(self):
        print(self.name,"is a good",self.role)

e1 = Employee('steven','developer')
e2 = Employee('david','IT analyst')
e3 = Employee('john','developer')

e1.emp_details()
e2.emp_details()
e3.emp_details()

In the above code, we created a class Employee and passed names (Steven) as an argument, this argument will pass to the __init__ method to initialize the object. The keyword self represents the instance of the class and binds attributes with given arguments.

READ:  How to convert a list to DataFrame in Python [9 ways]

The output of the code is shown below picture.

Python __init__ class
Python __init__ class code output

Inheritance in __init__ method

Inheritance is one of the important topics in OOPS. Inheritance is the ability to define a new class (Child class) that is a modified version of an existing class (Parent class). With Inheritance, we can understand that by the inheritance functionality, we can inherit the properties of one class to another.

In the below code, there is a syntax of how inheritance works.

class Parent_Class_Name:
 # Parent class code block
        
class Child_Class_Name(Parent_Class_Name):
 # Child_Class_Name

The Child class will inherit its properties and functionalities from the parent class and use the constructor of the parent class.

In the below code, it is shown how inheritance works with the __init__ method.

class employee:
    no_of_leaves = 8

    def __init__(self,name,designation):
        self.name = name
        self.deignation = designation

    def printdetails(self):
            return f"The name is {self.name}.Role is {self.role}"


class developer(employee):
    def __init__ (self,name,designation,skill):
        self.name = name
        self.desigantion = designation
        self.skill = skill

    def printdev(self):
        return f" the developer name is {self.name} and designation is {self.designation} and skills are {self.skill}"

john = employee("John","coder")
david = developer("david","programmer" ,"python")

print (david.no_of_leaves)

In the above code, we have defined one parent class named employee and one child class named developer. In the parent class, we defined the no_of_leaves, and since we have inherited the employee class in the developer class we will get the attribute no_of_leaves. therefore when we asked for no_of_leaves for the developer David it returned “8” from its parent class.

The output of the above code will be 8.

Execution of __init__ class in Python
Execution of __init__ class in Python

Conclusion

In this Python tutorial, we learned about the functionalities and use of the __init__ method in Python. We also touched the core topics like OOPS in Python and inheritance in Python.

READ:  Python List pop() method [With Examples]

In addition, we have also learned how in the __init__ method we used the inheritance method and executed it in our code and learned how inheritance work for the __init__ method.

You may also like to read the following articles: