Generic functions in Python
21 Mar 2018 | python3Definition
A generic function is a function defined for polymorphism.
Implementation
In Python3.6, you can achieve this using functools.singledispatch
:
from functools import singledispatch
@singledispatch
def myfunc(arg, verbose=False):
if verbose:
print('Length of strings')
print('This is not a string')
@myfunc.register(str)
def _(arg, verbose=False):
print(len(arg))
print(myfunc(3))
# >> 'This is not a string'
print(myfunc('abc'))
# >> 3
Comments