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!html
Feel free to add your suggestions. Please make sure example code conforms with PEP 8.python
Contentsgit
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.github
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'