A constructor is the first method that is call on object creation.

in addition ,The constructor is always call when  we create a new object.
therefore ,It can be use to initialize class variables and startup routines.

example :

In Python a constructor is also ,written as init(self).

class C:

      def __init__(self):
             print(‘Constructor called.’)

     def see(self):

obj = C()

as a result , output:

Constructor called.

Creating Constructor

as , After creating object  Changing variables is clunky and inefficient. also,then ,Not only do you waste time, but also,it bloats up your code too. This is especially true if you are creating multiple instances of the same class and altering each one.

therefore,also ,There is a concept in Object Oriented Programming called a


.in addition ,but also Constructors are special methods that allow an object to take arguments when we create it  and therefore , also ,automatically carry out actions. we usually use them  for assigning variables at the time of instantiation of object .

in addition ,In the case of Python, you create a constructor like you would any other method, using


, but also ,every constructor has the same name,


. therefore ,You can think of


 as being short for initialize because it is run when the object is initialized.

therefore ,Create a constructor for your car that takes in all of the variables as parameters and assigns them to instance variables with


.in addition , An instance variable is just a variable that exists in a specific instance of an object.

example :
def __init__(self, make, model, year, mileage, color):
          self.make = make self.model = model
          self.year = year self.mileage = mileage
          self.color = color

Types of Constructors

  1. Parameterized
  2. Non parameterized

Parameterized constructor :

in addition ,When we declare a constructor in such a way that it accepts the arguments during object creation then such type of constructors are known as Parameterized constructors. therefore ,As you can see that with such type of constructors we can pass the values (data) during object creation, which is used by the constructor to initialize the instance members of that object.

example :

class DemoClass:

      num = 101

# parameterized constructor

     def __init__(self, data): self .num = data

# a method

     def read_number(self):

           print(self .num)

# creating object of the class

# this will invoke parameterized constructor

obj = DemoClass(55)

# calling the instance method using the object obj


# creating another object of the class

obj2 = Demo Class(66)

# calling the instance method using the object obj

obj2 .read_number()

as a result ,Output:



Non-Parameterized Constructor

therefore ,When we want a constructors to do something but none of that is to manipulate values, therefore ,we can use a non-parameterized constructor.

example :

class demo:
         def __init__(self):
        def hello(self):
                print(f”Thank you for instantiating me, I’m all {self.color}. Would you like some {self.drink}? :)”)

as a result ,Output:

Thank you for instantiating me, I’m all red. Would you like some tea?

Python In-built class functions

  • getattr(obj,name,default): in addition ,It is used to access the attribute of the object.
  • setattr(obj, name,value):therefore It is used to set a particular value to the specific attribute of an       object.
  • delattr(obj, name):in addition ,It is used to delete a specific attribute.
  • hasattr(obj, name): therefore ,It returns true if the object contains some specific attribute.

Built-in class attributes

therefore ,Along with the other attributes, a python class also contains some built-in class attributes which provide information about the class.

__dict__ :  therefore ,It provides the dictionary containing the information about the class namespace.
__doc__ : in addition ,It contains a string which has the class documentation
__name__ :therefore ,It is use to access the class name.
__module__:It is use to access the module in which, this class is define.
__bases__ : therefore ,It contains a tuple including all base classes.

Advantages of Constructors:

  1. in addition ,constructors eliminates placing the default values.
  2. therefore , constructor eliminates calling the normal method implicitly.
  3. in addition , constructor is a special member method which will be call by the JVM implicitly for placing user/programmer define values instead of placing default values.
  4. therefore ,Constructors are meant for initializing the object.

Python Destructors 

Destructors are call when an object gets destroy. It’s the polar opposite of the constructor, which gets call on creation.

in addition ,also,Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

therefore ,also ,Destructor is defined using


keyword .

example :

class Vehicle:
      def __init__(self):
             print(‘Vehicle created.’)

     def __del__(self):
              print(‘Destructor called, vehicle deleted.’)

car = Vehicle()
del car

as a result ,Output :

Vehicle created.
Destructor called, vehicle deleted.

therefore , we have learn about , constructors and destructors in python also in addition we have learn about its types, in addition with its advantage and disadvantage . in addition we have learn about init method therefore how to create a constructors , as well as  all the examples.

%d bloggers like this: