__init__ in Python
Last Updated :
25 Jul, 2024
__init__ method in Python is used to initialize objects of a class. It is also called a constructor.
To completely understand the concept of __init__ method, you should be familiar with:
Prerequisites – Python Class and Objects, Self
What is __init__ in Python?
__init__ method is like default constructor in C++ and Java. Constructors are used to initialize the object’s state.
The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created.
Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. It is run as soon as an object of a class is instantiated.
The method is useful to do any initialization you want to do with your object.
Python
# A Sample class with init method
class Person:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
p = Person('Nikhil')
p.say_hi()
Output:
Hello, my name is Nikhil
Understanding the code
In the above example, a person named Nikhil is created. While creating a person, “Nikhil” is passed as an argument, this argument will be passed to the __init__ method to initialize the object.
The keyword self represents the instance of a class and binds the attributes with the given arguments. Similarly, many objects of the Person class can be created by passing different names as arguments.
Below is the example of __init__ in Python with parameters
Example of __init__ method in Python
Let’s look at some examples of __init__ method in Python.
Python
# A Sample class with init method
class Person:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
# Creating different objects
p1 = Person('Nikhil')
p2 = Person('Abhinav')
p3 = Person('Anshul')
p1.say_hi()
p2.say_hi()
p3.say_hi()
Output:
Hello, my name is Nikhil
Hello, my name is Abhinav
Hello, my name is Anshul
__init__ Method with Inheritance
Inheritance is the capability of one class to derive or inherit the properties from some other class. Let’s consider the below example to see how __init__ works in inheritance.
Python
# Python program to
# demonstrate init with
# inheritance
class A(object):
def __init__(self, something):
print("A init called")
self.something = something
class B(A):
def __init__(self, something):
# Calling init of parent class
A.__init__(self, something)
print("B init called")
self.something = something
obj = B("Something")
Output:
A init called
B init called
So, the parent class constructor is called first. But in Python, it is not compulsory that the parent class constructor will always be called first.
The order in which the __init__ method is called for a parent or a child class can be modified. This can simply be done by calling the parent class constructor after the body of the child class constructor.
Example:
Python
# Python program to
# demonstrate init with
# inheritance
class A(object):
def __init__(self, something):
print("A init called")
self.something = something
class B(A):
def __init__(self, something):
print("B init called")
self.something = something
# Calling init of parent class
A.__init__(self, something)
obj = B("Something")
Output:
B init called
A init called
Read: Inheritance in Python
We have covered __init__ in Python, discussed how to use __init__, and also saw some examples of using __init__ method in Python.
Constructor is a crucial concept in OOPs, and __init__ method is very similar to constructors.
Hope this article helped you in learning __init__ method, and you will be able to use it in your projects.
Related Article
__init__ in Python – FAQs
What is the __init__ Method in Python?
The __init__ method in Python is a special method called a constructor. It is automatically invoked when a new instance (object) of a class is created. The purpose of the __init__ method is to initialize the object’s attributes and perform any other setup necessary when an object is instantiated.
How is __init__ Used in Python Classes?
The __init__ method is defined within a class and typically includes the self parameter, which refers to the instance being created, followed by any other parameters needed to initialize the object.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Creating an instance of the Person class
person = Person("Alice", 30)
print(person.name) # Output: Alice
print(person.age) # Output: 30
In this example, the __init__ method initializes the name and age attributes of the Person class.
What are Constructors in Python?
Constructors in Python are special methods used to initialize newly created objects. The __init__ method is the constructor in Python. It allows you to set up the initial state of an object by assigning values to its attributes or performing other setup tasks.
How to Initialize a New Object in Python?
To initialize a new object in Python, you create an instance of a class, which automatically invokes the __init__ method.
Example:
class Dog:
def __init__(self, name, breed="Mixed", age=1):
self.name = name
self.breed = breed
self.age = age
# Creating an instance with default parameters
dog1 = Dog("Buddy")
print(dog1.name) # Output: Buddy
print(dog1.breed) # Output: Mixed
print(dog1.age) # Output: 1
# Creating an instance with specified parameters
dog2 = Dog("Max", "Golden Retriever", 5)
print(dog2.name) # Output: Max
print(dog2.breed) # Output: Golden Retriever
print(dog2.age) # Output: 5
What are the Default Parameters in __init__?
The __init__ method can include default parameter values. This allows you to create objects with default attribute values if no arguments are provided for those parameters.
Example:
class Dog:
def __init__(self, name, breed="Mixed", age=1):
self.name = name
self.breed = breed
self.age = age
# Creating an instance with default parameters
dog1 = Dog("Buddy")
print(dog1.name) # Output: Buddy
print(dog1.breed) # Output: Mixed
print(dog1.age) # Output: 1
# Creating an instance with specified parameters
dog2 = Dog("Max", "Golden Retriever", 5)
print(dog2.name) # Output: Max
print(dog2.breed) # Output: Golden Retriever
print(dog2.age) # Output: 5
In this example, the breed and age parameters have default values. If these parameters are not provided when creating a Dog object, the default values will be used.