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]
- The
self
argument 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
self
argument, 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. - The
__init__
method does not return anything, it only initializes the object’s attributes. - The
__init__
method is defined within the class block and is indented under the class definition.
The def
keyword is used to define a function, in this case, the __init__
method.__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
Employee
with an__init__
method that takes two arguments,name
andlocation
, and sets thename
andlocation
attributes of the object to the values of thename
andlocation
arguments respectively. - Two objects of the
Employee
class,employee1
andemployee2
, the__init__
method is automatically called and thename
andlocation
attributes of each object are set to the values passed as arguments while creating the objects. - Finally, we are printing the
name
andlocation
attributes 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
Employee
with an__init__
method that takes three arguments,name
,location
, andage
, and sets thename
,location
andage
attributes of the object to the values of thename
,location
andage
arguments respectively. - A method
set_age
which takes one argumentage
and sets the age attribute of the object to the value of theage
argument. - An object of the
Employee
class,employee1
, the__init__
method is automatically called and thename
,location
andage
attributes of the object are set to the values passed as arguments while creating the object. - Finally printing the
name
,location
andage
attributes 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 __init__
method:
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 x
and 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 x
and y
.
Conclusion
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.