Exercises
Decorators have a huge range of uses, so you can probably think of some yourself after reading this chapter, but you can easily elaborate on some of the decorators we wrote earlier:
- Extend the
track
function to monitor execution time. - Extend the
track
function with min/max/average execution time and call count. - Modify the memoization function to function with unhashable types.
- Modify the memoization function to have a cache per function instead of a global one.
- Create a version of
functools.cached_property
that can be recalculated as needed. - Create a single-dispatch decorator that considers all or a configurable number of arguments instead of only the first one.
- Enhance the
type_check
decorator to include additional checks such as requiring a number to be greater than or less than a given value.
Example answers for these exercises can be found on GitHub: https://github.com/mastering-python/exercises. You are...