...
/Type Hints and Overloaded Functions
Type Hints and Overloaded Functions
Let's learn about hints' types and overloaded functions.
We'll cover the following...
One obvious place in our code that we think Type Hints would work great is overloaded functions. Let’s grab the overloaded adding function we worked on previously and take a look at how much better it will be with type hints.
Example of overloaded function
Here’s the original code:
Press + to interact
from functools import singledispatch@singledispatchdef add(a, b):raise NotImplementedError('Unsupported type')@add.register(int)def _(a, b):print("First argument is of type ", type(a))print(a + b)@add.register(str)def _(a, b):print("First argument is of type ", type(a))print(a + b)@add.register(list)def _(a, b):print("First argument is of type ", type(a))print(a + b)if __name__ == '__main__':add(1, 2)add('Python', 'Programming')add([1, 2, 3], [5, 6, 7])
This example’s first argument is ...
Access this course and 1400+ top-rated courses and projects.