Implement C++ function template, Function Overloading & do-while loop in Python

This article contains information about how C++ function templates, function overloading and “do-while” loop can be implemented in Python Language. Let’s get started.

What is Function Overloading?

Function Overloading is a feature of C++ programming that allow users to have more than one function having same name but different parameter list or same parameter list but different parameter type. Function overloading is a type of “Polymorphism” and it happens at compile-time.
In Python, everything is dynamic because python is dynamically typed language means, python internally finds out which datatype we want to work on, without specifying its type but, there is also a different approach to achieve function overloading in python same as of C++.
To do this, we need to use “functools” in-built module of python.

What is functools?

The functools module is for higher-order functions, functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.
Some “functools” functions needed to implement function overloading are ::

Single Dispatch (i.e. singledispatch) :: A form of function dispatch where the implementation is chosen based on type of a single argument.

Register (i.e. register()):: It is an attribute used to add overloaded implementations to the function and acts as an decorator.

Decorators in Python :: A decorator in Python is a function that takes another function as its argument, and returns yet another function.

Given below is an example of function overloading in C++ and Python Language

Function Overloading in C++ ::

Function Overloading in Python ::

Drawback of this concept is that everything is done at compile time so, to make it at run-time, we need to use “Templates”.

What are Templates?

In C++, template is a concept which create generic functions, means, user can create only one function which will be strong enough to support all datatypes and perform required operations.

Python doesn’t support Templates but, we can apply some tricks to achieve it.

Given below is an example of using templates in C++ and Python Language,

Templates in C++ ::

Templates in Python (with tricks) ::

Loops

Loops are a useful and frequently used feature in all modern programming languages. If you want to automate a specific repetitive task or prevent yourself from writing repetitive code in your programs, using a loop is the best option for that.

Loops in Python

There are two types of loops built in python
1} for loop
2} while loop

for loop :: “for loop” in Python is used to iterate over a sequence, which could be a list, tuple, array, or string. It is used when you have a block of code which you want to repeat a fixed number of times.

while loop :: A “while loop” will run a piece of code while a condition is True. It will keep executing the desired set of code statements until that condition is no longer True.

What is do-while loop and how to implement it in Python?

Unique part of “do-while loops” is the fact that the code in the loop block will be executed at least one time. The code in the statement runs one time and then the condition is checked only after the code is executed.
So, the code runs once first and then the condition is checked. If the condition checked evaluates to true, the loop continues.

Syntax ::

Example to implement do-while loop in Python ::

Conclusion

You now know how to create & implement C++ templates, function overloading and do-while loop in Python Language.

Keep Learning…
Thank You!!