In this blog, we will learn everything about the __init__ python function. We will understand the init python function with several examples. So let’s get started.
what is __init__ python function?
In Python, the __init__ method is a special method that is automatically called when an object of a class is created. The
__init__ method is a constructor method in Python classes and is defined using the double underscore syntax, which indicates that it is a special method.
Syntax of __init__ python function
The syntax of the
__init__ python classes is as follows:
def __init__(self, [args]): [initialization code]
selfargument is passed as the first argument to the
__init__method, it is a reference to the object being created.
- Additional arguments can be passed after the
selfargument, which can be used to set the initial values of the attributes of the object. These additional arguments can be any valid Python data types, such as strings, integers, lists, etc.
__init__method does not return anything, it only initializes the object’s attributes.
__init__method is defined within the class block and is indented under the class definition.
def keyword is used to define a function, in this case, the
__init__ method is also a good place to do some extra setup for an object, such as opening a file, creating a connection to a database, etc.
__init__ python example
Below is an example of how the
__init__ method can be used in a Python class:
class Employee: def __init__(self, name, location): self.name = name self.location = location employee1 = Employee("Tom", "USA") employee2 = Employee("Jack", "India") print(employee1.name) print(employee1.location) print(employee1.name) print(employee2.location)
- The above code defines a class
__init__method that takes two arguments,
location, and sets the
locationattributes of the object to the values of the
- Two objects of the
__init__method is automatically called and the
locationattributes of each object are set to the values passed as arguments while creating the objects.
- Finally, we are printing the
locationattributes of both objects.
Call a function in __init__ python
In Python, it is possible to call a function within the
__init__ method of a class. You can call any function within the
__init__ method by using its name followed by parentheses.
class Employee: def __init__(self, name, location, age): self.name = name self.location = location self.age = age def set_age(self, age): self.age = age employee1 = Employee("Tom", "USA" ,30) print(employee1.name) print(employee1.location) print(employee1.age)
- A class
__init__method that takes three arguments,
age, and sets the
ageattributes of the object to the values of the
- A method
set_agewhich takes one argument
ageand sets the age attribute of the object to the value of the
- An object of the
__init__method is automatically called and the
ageattributes of the object are set to the values passed as arguments while creating the object.
- Finally printing the
ageattributes of the object.
Python class without __init__
In Python, a class can be defined without an
__init__ method. If a class does not have an
__init__ method, the objects of the class will not have any initial state when they are created. Below is an example of a class without a
class MyEmployee: pass employee1 = MyEmployee()
The MyEmployee class does not have an
__init__ method. When we create an object of the
MyEmployee class, the object does not have any initial state, and we can’t pass any arguments to it.
You can add attributes to the object after it’s created and also add methods to the class and use them on the created object.
employee1.name = "John" print(employee1.name)
In this case, you can add attributes to the object after it is created and you can also add methods to the class and use them on the created object.
It’s also worth noting that, even if a class doesn’t define a
__init__ method, it still inherits the
__init__ method of its parent class, if it has one.
__init__ method with inheritance in python
In Python, the init method is used to initialize the attributes of the object. When a class inherits from another class, the child class can use the init method of the parent class by calling the super() function. This allows the child class to inherit the attributes and behavior of the parent class, while also adding its own unique attributes and behavior.
Let’s take the below example
class my_parent_class: def __init__(self, x): self.x = x class my_child_class(my_parent_class): def __init__(self, x, y): super().__init__(x) self.y = y c = my_child_class(1, 2) print(c.x) print(c.y)
This code defines a class
my_parent_class with an
init python method that initializes the
x attribute. Then it defines a child’s class
my_child_class that inherits from
my_parent_class and has an
__init__ method that initializes
y attributes. It then creates an instance of
my_child_class and assigns the values 1 to
x and 2 to
y. And then it prints the values of
I hope you have liked this tutorial on the init python function. Please do let me know if you are facing any issues while following along.