
Validation with Metaprogramming Decorators in Python — Advanced Python
A Decorator is a special type of declaration that can be applied to a function to improve its functionality. This is also a type of metaprogramming. We’re attempting to change the functionality of a function at compile time. Metaprogramming is a fascinating concept in any programming language. It can be used to perform certain checks before executing the actual function. Let’s look at an example to better understand the use-case.
Assume we have a function divide that accepts both numerators and denominators. In this case, the numerators and denominators should be real numbers. and the numerator and denominator cannot be zero. Let’s put the function into action.
Explanation:
If you look at the example functions above, you will notice that we have added a few extra checks to verify the passed arguments. These additional checks can be implemented through metaprogramming without modifying the actual functions.