Multiple Constructors in Python

Multiple constructors are required when we want to have a different behavior of an object to perform the different actions on the object of a class.

The different behavior of an object can be achieved by providing the different parameters based on the type of parameters, number of parameters.

We can have a different behavior of an object and the multiple constructors by using concept of the positional arguments(*args).

Look at the below example to illustrate the behavior of multiple constructors using *args.

In the example when length of arguments it 1, it behaves like one constructor with one argument __init__(self, roll_number), similarly when length of arguments is 2 it behaves like the another constructor with 2 arguments __init__(self, roll_number, name).

Constructors in Python

Destructors in Python

Popular Posts

Author

Spread the knowledge
 
  

Leave a Reply

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