Dispatch Decorators in PythonDecorators are useful tool of the Python as they are used to modify the behavior of the function without permanently changing it. We have explained decorators in details in our decorator in Python tutorial. In this tutorial, we will learn about the dispatch decorators and its basic implementation. Before dive deep into this, let's have a simple example of the decorators. Example - Output: Hello, Message Before Execution We declare inside the function!! Hello, Message After Execution The above code is an example of the normal decorator in Python. The Nested Decorators has some problems, these are as below.
Now, let's understand how the dispatch decorator is different from the dispatch decorator. Dispatch DecoratorsDispatch decorators or functions are mechanism for performing different things based on signature, or list of types. It is used to select between different implementation of the same abstract method based on the signature, integer, string, or list of data types. As we know that, Python is a dynamically typed, so we need to specify that a function only takes some precise types as an argument. Let's understand the following example. Example - Output: 4 1.0 Hello Andrew! Explanation - In the above code, we imported the dispatch decorator from the multipledispatch module. The @dispatch(int) decorator specifies that the dispatcher 'func' is the value and allocates it to the ith index in the namespace (dictionary). Here is the new term comes Namespace, let's see what the namespace is? NamespaceA namespace is a dictionary that is used by the dispatch decorator. The dispatch decorator creates the dispatch object along with dispatch function and saves the object as the key-value pair. The dispatch decorator uses this dictionary to map a function as Dispatcher('func'). The namespace is used the global namespace in multipledispatch.core.global_namespace. We can also make own namespace using a dictionary. Let's understand the following example. Example - Output: 4 1.0 {'func': <dispatched func>} Note - To work with the dispatch decorator, we need to install the multiplesearch module using the below command.Single Dispatch DecoratorThe single dispatch decorator transforms a function into a single-dispatch generic function. When this is called, it:
Let's understand the following example for get its concept in better way. Example - Output: I am the default implementation. 'STRING' is a string. 1337 is an integer. [1, 3, 3, 7] is a list. It is different from other decorators and we can do many things using the single dispatcher function. ConclusionThis tutorial has covered one of the important types of the decorator - dispatch decorator. We have discussed its example and how it is different from the regular decorators. We used the multipledispatch and functools module. We need to install the multipledispatch module using the pip command. We mentioned some basic examples of dispatch however it can perform much complex tasks. Next TopicIntrospection in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India