Functools: functools.singledispatch
Explore how to use the functools singledispatch decorator in Python to create generic functions that dispatch based on the first argument's type. Understand how to register handlers for various types and handle unsupported types effectively.
Overview of singledispatch module
Python recently added partial support for function overloading in Python 3.4. They did this by adding a neat little decorator to the
functools module called singledispatch. This decorator will transform our regular function into a single dispatch generic function. Note
however that singledispatch only happens based on the first argument’s
type.
Simple example of singledispatch()
Let’s take a look at an example to see how this works!
Here we import ...