closure.py 468 B

12345678910111213141516171819202122232425262728293031
  1. # Closures
  2. import logging
  3. logging.basicConfig(filename='example.log', level=logging.INFO)
  4. def logger(func):
  5. def log_func(*args):
  6. logging.info(
  7. 'Running "{}" with arguments {}'.format(func.__name__, args))
  8. print(func(*args))
  9. return log_func
  10. def add(x, y):
  11. return x+y
  12. def sub(x, y):
  13. return x-y
  14. add_logger = logger(add)
  15. sub_logger = logger(sub)
  16. add_logger(3, 3)
  17. add_logger(4, 5)
  18. sub_logger(10, 5)
  19. sub_logger(20, 10)