Python Constructor

A constructor is used to initialize a class when its instance is created. The first parameter of the constructor is always “self”. Constructor in Python is created using a special function known as  __init__() function.


How to Create a  constructor in Python?

A constructor in Python can be created using __init__() function that can take any number of parameters with self as a first parameter. Let us consider the following example to create an Employee class with three attributes, id, name, and department.

The output of the above program is:-

ID: 1
Name: John Doe
Department: Web Development
ID: 2
Name: Ryan Rees
Department: Mobile Development
The example given above is of the parameterized constructor. Python can also have no parameterized constructor where the constructor doesn’t receive any parameters.


This is non parametrized constructor
Hello John Snow
SHARE Python Constructor

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *