Posts tagged python

Generic Function in Python with Singledispatch

2018-03-27

Imagine, you can write different implementations of a function of the same name in the same scope, depending on the types of arguments. Wouldn’t it be great? Of course, it would be. There is a term for this. It is called “Generic Function”. Python recently added support for generic function in Python 3.4 (PEP 443). They did this to the module by adding decorator. What is Singledispatch? At this point, you may be wondering what is . Okay, let’s go with generic function again. A generic function is composed of multiple functions implementing the same operation for different types. Which implementation should be used during a call is determined by the dispatch algorithm. When the implementation is chosen based on the type of a single argument, this is known as single dispatch. In Python, implementation is chosen based on the type of the first argument of function. So in simple, you define a default function and then register additional versions of that functions depending on the type of the first argument. Singledispatch in Action Let’s see in action. There are few steps for writing a generic function with . Import from . Define an default or fallback function with decorator. It’s our generic function. Then, Register additional implementations by passing the type in attribute of the generic function. It’s a decorator, so you decorate your implementations like this . You can also register lambdas and pre-existing functions. Now, we will implement a generic function called , which will print something in a formatted way based on the type. For it will print index and value along with type of value and for it will print key-value pair along with their type etc. By default it will print the passed argument along with it’s type. Let’s define our default function first. I am not going to explain the implementation. It’s fairly basic. It’s the default or fallback implementation of our generic function. We define a function and decorate it with decorator. If there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with is registered for the base object type, which means it is used if no better implementation is found. Remember the next step? Now, it’s time for registering the overloaded implementations. Let’s implement for first. In case you are wondering why I am using as name. It’s because, I want only one generic function. If you give it a name…