In Python, a constructor is a special method used to initialize objects when a class instance is created. The constructor ensures that the object’s data members are assigned appropriate values right at the time of instantiation. The method responsible for this is called __init__() in Python, which is automatically invoked whenever you create a new object.
Syntax of init() in Python
The first argument of the __init__() method is always self, which refers to the current instance of the class. Constructor may or may not have other input parameters i.e. other input parameters are optional.
class MyClass:
def __init__(self, input_parameters):
# initialization code
self.value = input_parameters
Python __init__() method and object initialization
In Python when you create a new instance of a class, for example, obj = MyClass() first the special method __new__() is called to create the object, and then the special method __init__() is called to initialize it.
This process happens implicitly on instantiating a class, so you don’t need to call the constructor manually.
Types of constructors in Python
Constructor in Python can be of two types-
- Non-parameterized constructor- Constructor without any parameters except self which is a reference to the current object. Used when you don’t need to pass any external values during object creation.
class Demo: def __init__(self): self.message = "Default Constructor" - Parameterized constructor- Accepts additional arguments besides self. Useful when you want to assign specific values to data members at the time of initialization. In that case first argument is self and other arguments are used for initialization.
class Demo: def __init__(self, name): self.name = name
Non-parameterized Python constructor example
class Person:
def __init__(self):
self.name = "David"
self.age = 32
def display_data(self):
print(self.name)
print(self.age)
person = Person()
person.display_data()
Output
David 32
Here constructor has only one parameter 'self'. Using self you can access the fields of the class and initialize them. This constructor is called implicitly when an instance of the class is created.
person = Person()
Empty parenthesis after the class means we are not passing any arguments to the constructor.
Parameterized Python constructor example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_data(self):
print(self.name)
print(self.age)
person = Person('David', 32)
person.display_data()
Output
David 32
Here constructor has arguments. When instance of the class is created two arguments corresponding to constructor parameters are passed.
person = Person('David', 32)
Constructor in Python with default values
You can also assign default values to the constructor arguments. If no value is passed while instantiating the class, default values are assigned to the instance variables.
class Person:
def __init__(self, name='Unknown', age=0):
self.name = name
self.age = age
def display_data(self):
print(self.name)
print(self.age)
person = Person()
person.display_data()
Output
Unknown 0
That's all for this topic Constructor in Python – Learn How init() Works. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment