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.
- Memoization using decorators in Python
- Python IMDbPY - Error Handling
- How to preserve Function Metadata while using Decorators?
- Handling 404 Error in Flask
- Function Decorators in Python | Set 1 (Introduction)
- Decorators in Python
- Decorators with parameters in Python
- Useful cases to illustrate Decorators in python
- Nested Decorators in Python
- Chain Multiple Decorators in Python
- Python Decorators: A Complete Guide
- Conditional Decorators in Python
- Debugging decorators in Python
- SQL using Python | Set 3 (Handling large data)
- Precision Handling in Python
- Python Exception Handling
- Multiple Exception Handling in Python
- ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python
- Python | Handling recursion limit
- Python | Handling no element found in index()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.