w3resource

Python Decorator: Exercises, Solutions, and Practice


This resource offers a total of 60 Python Decorator problems for practice. It includes 12 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

Python decorators are a way to modify a function or class's behavior without directly modifying its code. They allow you to wrap a function or class around another function, adding additional functionality before, after, or around the original function's execution. Decorators are denoted by the @ symbol followed by the name of the decorator function. They provide a concise and reusable way to extend Python code functionality. They are commonly used for logging, authentication, caching, and more.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Create a Decorator to Log Function Arguments and Return Value

Write a Python program to create a decorator that logs the arguments and return value of a function.

Click me to see the sample solution


2. Create a Decorator to Measure Function Execution Time

Write a Python program to create a decorator function to measure the execution time of a function.

Click me to see the sample solution


3. Create a decorator to convert function return value type

Write a Python program to create a decorator to convert the return value of a function to a specified data type.

Click me to see the sample solution


4. Implement a Decorator to Cache Function Results

Write a Python program that implements a decorator to cache the result of a function.

Click me to see the sample solution


5. Implement a Decorator to Validate Function Arguments

Write a Python program that implements a decorator to validate function arguments based on a given condition.

Click me to see the sample solution


6. Implement a Decorator to Retry a Function on Failure

Write a Python program that implements a decorator to retry a function multiple times in case of failure.

Click me to see the sample solution


7. Implement a Decorator to Enforce Rate Limits on a Function

Write a Python program that implements a decorator to enforce rate limits on a function.

Click me to see the sample solution


8. Implement a Decorator to Add Logging Functionality

Write a Python program that implements a decorator to add logging functionality to a function.

Click me to see the sample solution


9. Implement a decorator to handle exceptions with a default value

Write a Python program that implements a decorator to handle exceptions raised by a function and provide a default response.

Click me to see the sample solution


10. Implement a decorator to enforce type checking on arguments

Write a Python program that implements a decorator to enforce type checking on the arguments of a function.

Click me to see the sample solution


11. Implement a Decorator to Measure Memory Usage of a Function

Write a Python program that implements a decorator to measure the memory usage of a function.

Click me to see the sample solution


12. Implement a Decorator for Caching with Expiration Time

Write a Python program that implements a decorator to provide caching with expiration time for a function.

Click me to see the sample solution


Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.