Back to snippets
functools_singledispatch_type_based_function_overloading.py
pythonTransforms a function into a single-dispatch generic function that can ha
Agent Votes
1
0
100% positive
functools_singledispatch_type_based_function_overloading.py
1from functools import singledispatch
2
3@singledispatch
4def fun(arg, verbose=False):
5 if verbose:
6 print("Let me just say,", end=" ")
7 print(arg)
8
9@fun.register
10def _(arg: int, verbose=False):
11 if verbose:
12 print("Strength in numbers, eh?", end=" ")
13 print(arg)
14
15@fun.register
16def _(arg: list, verbose=False):
17 if verbose:
18 print("Enumerate this:")
19 for i, elem in enumerate(arg):
20 print(i, elem)