decorator.py 610 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import functools
  4. def log(func):
  5. @functools.wraps(func)
  6. def wrapper(*args, **kw):
  7. print('call %s():' % func.__name__)
  8. return func(*args, **kw)
  9. return wrapper
  10. @log
  11. def now():
  12. print('2015-3-25')
  13. now()
  14. def logger(text):
  15. def decorator(func):
  16. @functools.wraps(func)
  17. def wrapper(*args, **kw):
  18. print('%s %s():' % (text, func.__name__))
  19. return func(*args, **kw)
  20. return wrapper
  21. return decorator
  22. @logger('DEBUG')
  23. def today():
  24. print('2015-3-25')
  25. today()
  26. print(today.__name__)