**Prerequisite – ****Constructors****, ****@classmethod decorators**

Python does not support explicit multiple constructors, yet there are some ways using which the multiple constructors can be achieved. If multiple **__init__** methods are written for the same class, then the latest one overwrites all the previous constructors. Look at the example below.

## Python3

`class` `example: ` ` ` ` ` `def` `__init__(` `self` `): ` ` ` `print` `(` `"One"` `) ` ` ` ` ` `def` `__init__(` `self` `): ` ` ` `print` `(` `"Two"` `) ` ` ` ` ` `def` `__init__(` `self` `): ` ` ` `print` `(` `"Three"` `) ` ` ` ` ` `e ` `=` `example() ` |

*chevron_right*

*filter_none*

**Output**

Three

## Need for multiple constructors

Multiple constructors are required when one has to perform different actions on the instantiation of a class. This is useful when the class has to perform different actions on different parameters. The class constructors can be made to exhibit polymorphism in three ways which are listed below.

- Overloading constructors based on arguments.
- Calling methods from
**__init__**. - Using
**@classmethod**decorator.

This article explains how to have multiple constructors in a clean and Pythonic way with examples.

### Overloading constructors based on arguments

The constructor overloading is done by checking conditions for the arguments passed and performing required actions. For example, consider passing an argument to the class **sample**,

- If the parameter is an
**int**, the square of the number should be the answer. - If the parameter is a
**String**, the answer should be**“Hello!!”+string.** - If the parameter is of
**length greater than 1,**the sum of arguments should be stored as the answer.

## Python3

`class` `sample: ` ` ` ` ` `# constructor overloading ` ` ` `# based on args ` ` ` `def` `__init__(` `self` `, ` `*` `args): ` ` ` ` ` `# if args are more than 1 ` ` ` `# sum of args ` ` ` `if` `len` `(args) > ` `1` `: ` ` ` `self` `.ans ` `=` `0` ` ` `for` `i ` `in` `args: ` ` ` `self` `.ans ` `+` `=` `i ` ` ` ` ` `# if arg is an integer ` ` ` `# square the arg ` ` ` `elif` `isinstance` `(args[` `0` `], ` `int` `): ` ` ` `self` `.ans ` `=` `args[` `0` `]` `*` `args[` `0` `] ` ` ` ` ` `# if arg is string ` ` ` `# Print with hello ` ` ` `elif` `isinstance` `(args[` `0` `], ` `str` `): ` ` ` `self` `.ans ` `=` `"Hello! "` `+` `args[` `0` `]` `+` `"."` ` ` ` ` `s1 ` `=` `sample(` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `) ` `print` `(` `"Sum of list :"` `, s1.ans) ` ` ` `s2 ` `=` `sample(` `5` `) ` `print` `(` `"Square of int :"` `, s2.ans) ` ` ` `s3 ` `=` `sample(` `"GeeksforGeeks"` `) ` `print` `(` `"String :"` `, s3.ans) ` |

*chevron_right*

*filter_none*

**Output**

Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.

In the code above, the instance variable was **ans**, but its values differ based on the arguments. Since a variable number of arguments for the class, ***args** is used which is a tuple that contains the arguments passed and can be accessed using an index. In the case of int and string, only one argument is passed and thus accessed as args[0] (the only element in the tuple).

### Calling methods from __init__

A class can have one constructor __init__ which can perform any action when the instance of the class is created. This constructor can be made to different functions that carry out different actions based on the arguments passed. Now consider an example :

- If the number of arguments passed is 2, then evaluate the expression
**x = a**^{2}-b^{2} - If the number of arguments passed is 3, then evaluate the expression
**y = a**^{2}+b^{2}-c. - If
**sum up the squares, divide it by the****highest value in the arguments passed.**

## Python3

`class` `eval_equations: ` ` ` ` ` `# single constructor to call other methods ` ` ` `def` `__init__(` `self` `, ` `*` `inp): ` ` ` ` ` `# when 2 arguments are passed ` ` ` `if` `len` `(inp) ` `=` `=` `2` `: ` ` ` `self` `.ans ` `=` `self` `.eq2(inp) ` ` ` ` ` `# when 3 arguments are passed ` ` ` `elif` `len` `(inp) ` `=` `=` `3` `: ` ` ` `self` `.ans ` `=` `self` `.eq1(inp) ` ` ` ` ` `# when more than 3 arguments are passed ` ` ` `else` `: ` ` ` `self` `.ans ` `=` `self` `.eq3(inp) ` ` ` ` ` `def` `eq1(` `self` `, args): ` ` ` `x ` `=` `(args[` `0` `]` `*` `args[` `0` `])` `+` `(args[` `1` `]` `*` `args[` `1` `])` `-` `args[` `2` `] ` ` ` `return` `x ` ` ` ` ` `def` `eq2(` `self` `, args): ` ` ` `y ` `=` `(args[` `0` `]` `*` `args[` `0` `])` `-` `(args[` `1` `]` `*` `args[` `1` `]) ` ` ` `return` `y ` ` ` ` ` `def` `eq3(` `self` `, args): ` ` ` `temp ` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(args)): ` ` ` `temp ` `+` `=` `args[i]` `*` `args[i] ` ` ` ` ` `temp ` `=` `temp` `/` `max` `(args) ` ` ` `z ` `=` `temp ` ` ` `return` `z ` ` ` ` ` `inp1 ` `=` `eval_equations(` `1` `, ` `2` `) ` `inp2 ` `=` `eval_equations(` `1` `, ` `2` `, ` `3` `) ` `inp3 ` `=` `eval_equations(` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `) ` ` ` `print` `(` `"equation 2 :"` `, inp1.ans) ` `print` `(` `"equation 1 :"` `, inp2.ans) ` `print` `(` `"equation 3 :"` `, inp3.ans) ` |

*chevron_right*

*filter_none*

**Output**

equation 2 : -3 equation 1 : 2 equation 3 : 11.0

In the example above, the equation to be evaluated is written on different instance methods and made to return the answer. The constructor calls the appropriate method and acts differently for different parameters.

The expressions have been evaluated as follows:

inputs : 1,2 —> 1

^{2}-2^{2}= 1-4 = -3

inputs : 1,2,3 —> (1

^{2 }+^{ }2^{2}) – 3 = 5-3 = 2inputs : 1,2,3,4,5 —> (1

^{2}+ 2^{2}+ 3^{2 }+ 4^{2}+ 5^{2}) / 5 = 55/5 = 11.0

### Using @classmethod decorator

This decorator allows a function to be accessible without instantiating the class. The functions can be accessed both by the instance of the class and the class itself. The first parameter of the method that is declared as **classmethod **is **cls, **which is like the **self**** **of the instance methods. Here **cls **refer to the class itself. This proves to be very helpful to use multiple constructors in Python and is a more Pythonic approach considered to the above ones. Consider the same example used above. Evaluate different expressions based on the number of inputs.

## Python3

`class` `eval_equations: ` ` ` ` ` `# basic constructor ` ` ` `def` `__init__(` `self` `, a): ` ` ` `self` `.ans ` `=` `a ` ` ` ` ` `# expression 1 ` ` ` `@classmethod` ` ` `def` `eq1(` `cls` `, args): ` ` ` ` ` `# create an object for the class to return ` ` ` `x ` `=` `cls` `((args[` `0` `]` `*` `args[` `0` `])` `+` `(args[` `1` `]` `*` `args[` `1` `])` `-` `args[` `2` `]) ` ` ` `return` `x ` ` ` ` ` `# expression 2 ` ` ` `@classmethod` ` ` `def` `eq2(` `cls` `, args): ` ` ` `y ` `=` `cls` `((args[` `0` `]` `*` `args[` `0` `])` `-` `(args[` `1` `]` `*` `args[` `1` `])) ` ` ` `return` `y ` ` ` ` ` `# expression 3 ` ` ` `@classmethod` ` ` `def` `eq3(` `cls` `, args): ` ` ` `temp ` `=` `0` ` ` ` ` `# square of each element ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(args)): ` ` ` `temp ` `+` `=` `args[i]` `*` `args[i] ` ` ` ` ` `temp ` `=` `temp` `/` `max` `(args) ` ` ` `z ` `=` `cls` `(temp) ` ` ` `return` `z ` ` ` ` ` `li ` `=` `[[` `1` `, ` `2` `], [` `1` `, ` `2` `, ` `3` `], [` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `]] ` `i ` `=` `0` ` ` `# loop to get input three times ` `while` `i < ` `3` `: ` ` ` ` ` `inp ` `=` `li[i] ` ` ` ` ` `# no.of.arguments = 2 ` ` ` `if` `len` `(inp) ` `=` `=` `2` `: ` ` ` `p ` `=` `eval_equations.eq2(inp) ` ` ` `print` `(` `"equation 2 :"` `, p.ans) ` ` ` ` ` `# no.of.arguments = 3 ` ` ` `elif` `len` `(inp) ` `=` `=` `3` `: ` ` ` `p ` `=` `eval_equations.eq1(inp) ` ` ` `print` `(` `"equation 1 :"` `, p.ans) ` ` ` ` ` `# More than three arguments ` ` ` `else` `: ` ` ` `p ` `=` `eval_equations.eq3(inp) ` ` ` `print` `(` `"equation 3 :"` `, p.ans) ` ` ` ` ` `#increment loop ` ` ` `i ` `+` `=` `1` |

*chevron_right*

*filter_none*

**Output**

equation 2 : -3 equation 1 : 2 equation 3 : 11.0

In the example above, the instance of the object is not created initially. The class methods to evaluate various expression has been defined with **@classmethod **decorator. Now they can be called with the class name and the object is created in that class method after evaluating the expression. The instance variable holds different answers for a different number of parameters passed.

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Defining Clean Up Actions in Python
- Clean the string data in the given Pandas Dataframe
- Constructors in Python
- Python List Comprehension | Three way partitioning of an array around a given range
- Python | Using 2D arrays/lists the right way
- Best Way To Start Learning Python - A Complete Roadmap
- Change your way to put logic in your code - Python
- Different way to create a thread in Python
- Fastest way to Convert Integers to Strings in Pandas DataFrame
- Python | Check if two lists have at-least one element common
- Python | Check if two lists have any element in common
- Python | Find whether all tuple have same length
- Python - Check if all tuples have element difference less than K
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python program to extract only the numbers from a list which have some specific digits
- Python Program that prints rows from the matrix that have same element at a given index
- Python - Test if Rows have Similar frequency
- Check if both halves of the string have same set of characters in Python
- PyQt5 - Calculator for Number of seconds You have Lived
- Create an n x n square matrix, where all the sub-matrix have the sum of opposite corner elements as even

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.