Decorators in Python is one of the most useful concepts supported by Python. It takes functions as arguments and also has a nested function. They extend the functionality of the nested function.
Decorator called inner function outter function
Error handling using decorators
The following example shows how a general error handling code without use of any decorator looks like:
4.5 441 0.5 wrong data types. enter numeric
Even though there is nothing logically wrong with the above code but it lacks clarity. To make the code more clean and efficient decorators are used for error handling. The following example depicts how the above code can be more presentable and understandable by use of decorators:
Square wrong data types. enter numeric
Divide wrong data types. enter numeric
Mean wrong data types. enter numeric
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course