In OOP, constructors are special methods that are used to initialize objects of a given class. They provide a way to perform initialization tasks such as setting default values and assigning the necessary initial data to the object being created.
In Python,the constructor method is __init__
()
.It is always the first instance method that is called when an object of a class is created.
Python automatically calls the __init__()
method whenever an object is created. The method serves to give the object the initial state needed before it can be used.
The __init__()
method belongs to a set of methods known as dunder(double underscore) or magic methods.
Syntax and Example
class class_name:
def __init__(self, <other_arguments>):
#Necessary initialisation for initial values
#Other methods
class Example:
def __init__(self):
print("Hello, World!")
e = Example()
The __init__()
method, like all other instance methods , always takes self
as the mandatory first parameter. Python automatically inserts the reference to the current instance of the class as the first argument. Meaning that when you create an instance from a class, the instance itself is passed as the first argument to the __init__()
method. This makes it possible to access, assign and modify instance attributes from within the class.
__init__() constructor Should always return None.
The __init__()
method should never return any value apart from the default, None
. On an
attempt to return anything other than None, a TypeError
is raised.
class Example:
def __init__(self):
return 1
e = Example()
Parameterized __init__ Constructor
When the __init__()
method in a class specifies other parameters alongside self
, the parameters should be passed as arguments when initializing an object.
#Define the class
class Person:
#Define the constructor method
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("John", 28)
print(p.name)
print(p.age)
__init__ constructor with default values
Just like any other function or method, the __init__()
constructor can be defined with default arguments. In such a case, the default values will be used unless other values are specified when instantiating an object.
class Student:
def __init__(self, name, student_id, department = "Computer Science"):
self.name = name
self.department = department
a = Student("John", 12345678)
b = Student("Mary", 98765432, "School of law")
print(a.department)
print(b.department)
In the above example, all objects created from the class "Student" acquires a default value for department
parameter. That value is only overridden when a Student object specifies a different value.
Note that there should only be one constructor function in a given class. If more than one __init__ ()
methods are found within a class, only the latest will be used.
Conclusion
- Constructors are special methods that are called when class instances are being created.
__init__()
method serves as the constructor in Python classes.- The
__init__ ()
method is called automatically when we initialize an object. - If the
__init__()
methods takes arguments, we pass the required arguments when initializing objects. - There can only be one
__init__()
method in a given class.