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

The constructor is always called when creating a new object.
It can be used to initialize class variables and startup routines.

Related course: Complete Python Bootcamp: Go from zero to hero in Python 3

python constructor example

In the example below we create a class C with a constructor and the method see. \

We create one object C. On execution it will output both lines because we call the method and when creating the object it calls the constructor.

In Python a constructor is written as init(self)

class C:
def __init__(self):
print('Constructor called.')

def see(self):

obj = C()

This will output:

Constructor called.

Watch how the output of the constructor is shown first.