Dispatch Decorator in Python
Last Updated :
08 May, 2020
Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it.
Example:
Python3
# defining a decorator
def hello_decorator(func):
# inner1 is a Wrapper function in
# which the argument is called
# inner function can access the outer local
# functions like in this case "func"
def inner1():
print("Hello, this is before function execution")
# calling the actual function now
# inside the wrapper function.
func()
print("This is after function execution")
return inner1
# defining a function, to be called inside wrapper
def function_to_be_used():
print("This is inside the function !!")
# passing 'function_to_be_used' inside the
# decorator to control its behavior
function_to_be_used = hello_decorator(function_to_be_used)
# calling the function
function_to_be_used()
Output:
Hello, this is before function execution
This is inside the function !!
This is after function execution
Dispatch Decorator
A Dispatch decorator is used to select between different implementations of the same abstract method based on the signature, or list of types.
Example:
Python3
# Python program to demonstrate
# dispatch decorator
from multipledispatch import dispatch
@dispatch(int)
def func(x):
return x * 2
@dispatch(float)
def func(x):
return x / 2
# Driver code
print(func(2))
print(func(2.0))
Output:
4
1.0
In the above example, the
@dispatch(int)
statement suggests that the Dispatcher 'func' is created and then allocates the 'int' type as the key and dispatcher 'func' as the value and allocates it to the ith index in the namespace (dictionary).
Now you all must be wondering what is a namespace? Don't worry let's have a look at the namespace.
Namespace
A namespace is nothing but a dictionary that is used by the dispatch decorator. The dispatch decorator creates a dispatcher object with the name of the function and stores this object as a key-value pair. This dictionary is used to map a functions like
func
in the above example to a dispatcher object like
Disptacher('func')
.
By default, the namespace used is the global namespace in
multipledispatch.core.global_namespace
. For additional security, one can establish their own namespaces using a dictionary.
Example:
Python3
from multipledispatch import dispatch
nsp = {}
@dispatch(int, namespace = nsp)
def func(x):
return x * 2
@dispatch(float, namespace = nsp)
def func(x):
return x / 2
# Driver code
print(func(2))
print(func(2.0))
print(nsp)
Output:
4
1.0
{'func': <dispatched func>}
Similar Reads
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
call() decorator in Python Python Decorators are important features of the language that allow a programmer to modify the behavior of a class. These features are added functionally to the existing code. This is a type of metaprogramming when the program is modified at compile time. The decorators can be used to inject modifie
3 min read
Nested Decorators in Python Everything in Python is an object. Even function is a type of object in Python. Decorators are a special type of function which return a wrapper function. They are considered very powerful in Python and are used to modify the behaviour of a function temporarily without changing its actual value. Nes
2 min read
Generators in Python Python generator functions are a powerful tool for creating iterators. In this article, we will discuss how the generator function works in Python.Generator Function in PythonA generator function is a special type of function that returns an iterator object. Instead of using return to send back a si
5 min read
Descriptor in Python In Python, a descriptor is any object that implements at least one of the following methods: __get__(self, instance, owner), __set__(self, instance, value), or __delete__(self, instance). When a class defines any of these methods, its instances become descriptors. Descriptors act as intermediaries i
5 min read