Thursday, December 17, 2020

Constructor in Python - __init__() function

Constructor is a special method provided to initialize objects (assign values to its data members) when they are created. In Python __init__() special method is the constructor and this method is always called when an object is created.

Syntax of init() in Python

First argument in __init__() method is always self. Constructor may or may not have other input parameters i.e. other input parameters are optional.

def __init__(self, input_parameters):
 #initialization code
 ....
 ....

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.

The constructor is called implicitly on instantiating a class.

Types of constructors in Python

Constructor in Python can be of two types-

  1. Non-parameterized constructor- Constructor without any parameters except self which is a reference to the current object.
  2. Parameterized constructor- When you want to assign specific values to the data members while initializing an object you can pass them as arguments to __init__() method. In that case first argument is self and other arguments are used for initialization.

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 - __init__() Function. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python Installation on Windows
  2. Class And Object in Python
  3. Inheritance in Python
  4. Python continue Statement With Examples
  5. Local, Nonlocal And Global Variables in Python

You may also like-

  1. Method Overriding in Python
  2. Changing String Case in Python
  3. Keyword Arguments in Python
  4. Python Program to Display Prime Numbers
  5. Constructor Chaining in Java
  6. ArrayList in Java With Examples
  7. Spring Constructor Based Dependency Injection
  8. Installing Ubuntu Along With Windows

No comments:

Post a Comment