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
Creating Well-Behaved Decorators / "Decorator decorator"
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 def simple_decorator(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 def new_decorator(f):
12 g = decorator(f)
13 g.__name__ = f.__name__
14 g.__doc__ = f.__doc__
15 g.__dict__.update(f.__dict__)
16 return g
17 # Now a few lines needed to make simple_decorator itself
18 # be a well-behaved decorator.
19 new_decorator.__name__ = decorator.__name__
20 new_decorator.__doc__ = decorator.__doc__
21 new_decorator.__dict__.update(decorator.__dict__)
22 return new_decorator
23
24 #
25 # Sample Use:
26 #
27 @simple_decorator
28 def my_simple_logging_decorator(func):
29 def you_will_never_see_this_name(*args, **kwargs):
30 print 'calling {}'.format(func.__name__)
31 return func(*args, **kwargs)
32 return you_will_never_see_this_name
33
34 @my_simple_logging_decorator
35 def double(x):
36 'Doubles a number.'
37 return 2 * x
38
39 assert double.__name__ == 'double