This page is meant to be a central repository of decorator code pieces, whether useful or not <wink>. It is NOT a page to discuss decorator syntax!
Feel free to add your suggestions. Please make sure example code conforms with PEP 8.
Contents
- Creating Well-Behaved Decorators / "Decorator decorator"
- Property Definition
- Memoize
- Alternate memoize as nested functions
- Alternate memoize as dict subclass
- Alternate memoize that stores cache between executions
- Cached Properties
- Retry
- Pseudo-currying
- Creating decorator with optional arguments
- Controllable DIY debug
- Easy adding methods to a class instance
- Counting function calls
- Alternate Counting function calls
- Generating Deprecation Warnings
- Smart deprecation warnings (with valid filenames, line numbers, etc.)
- Ignoring Deprecation Warnings
- Enable/Disable Decorators
- Easy Dump of Function Arguments
- Pre-/Post-Conditions
- Profiling/Coverage Analysis
- Line Tracing Individual Functions
- Synchronization
- Type Enforcement (accepts/returns)
- CGI method wrapper
- State Machine Implementaion
- C++/Java-keyword-like function decorators
- Different Decorator Forms
- Unimplemented function replacement
- Redirects stdout printing to python standard logging.
- Access control
- Events rising and handling
- Singleton
- The Sublime Singleton
- Asynchronous Call
- Class method decorator using instance
- Another Retrying Decorator
- Logging decorator with specified logger (or default)
- Lazy Thunkify
- Aggregative decorators for generator functions
- Function Timeout
Note: This is only one recipe. Others include inheritance from a standard decorator (link?), the functools @wraps decorator, and a factory function such as Michele Simionato's decorator module which even preserves signature information.
Toggle line numbers
1 decorator):
2 '''This decorator can be used to turn simple functions
3 into well-behaved decorators, so long as the decorators
4 are fairly simple. If a decorator expects a function and
5 returns a function (no descriptors), and if it doesn't
6 modify function attributes or docstring, then it is
7 eligible to use this. Simply apply @simple_decorator to
8 your decorator and it will automatically preserve the
9 docstring and function attributes of functions to which
10 it is applied.'''
11 f):
12 f)
13 __name__
14 __doc__
15 __dict__)
16 g
17 # Now a few lines needed to make simple_decorator itself
18 # be a well-behaved decorator.
19 __name__
20 __doc__
21 __dict__)
22 new_decorator
23
24 #
25 # Sample Use:
26 #
27 @simple_decorator
28 func):
29 kwargs):
30 __name__)
31 kwargs)
32 you_will_never_see_this_name
33
34 @my_simple_logging_decorator
35 x):
36 '
37 x
38
39 __name__ ==