decorator.py 748 B

1234567891011121314151617181920212223242526272829303132
  1. """https://docs.python.org/2/library/functools.html#functools.wraps"""
  2. """https://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python/739665#739665"""
  3. from functools import wraps
  4. def makebold(fn):
  5. @wraps(fn)
  6. def wrapped():
  7. return "<b>" + fn() + "</b>"
  8. return wrapped
  9. def makeitalic(fn):
  10. @wraps(fn)
  11. def wrapped():
  12. return "<i>" + fn() + "</i>"
  13. return wrapped
  14. @makebold
  15. @makeitalic
  16. def hello():
  17. """a decorated hello world"""
  18. return "hello world"
  19. if __name__ == '__main__':
  20. print('result:{} name:{} doc:{}'.format(hello(), hello.__name__, hello.__doc__))
  21. ### OUTPUT ###
  22. # result:<b><i>hello world</i></b> name:hello doc:a decorated hello world