fork(1) download
  1. class Logger(object):
  2. def __init__(self, colour):
  3. self.colour = colour
  4.  
  5. def log(self, msg):
  6. print(self.colour + ": " + msg)
  7.  
  8.  
  9. def logger_choice(colour):
  10. def col_decor(f):
  11. def f_decor(*args, **kwargs):
  12. global dprint
  13. prev_dprint = dprint
  14. dprint = loggers[colour].log
  15. f(*args, **kwargs)
  16. dprint = prev_dprint
  17. return f_decor
  18. return col_decor
  19.  
  20. def f():
  21. dprint("this should be black")
  22.  
  23. @logger_choice(colour='yellow')
  24. def fu(x):
  25. dprint("this should be yellow, and 5 = %d" % x)
  26.  
  27. @logger_choice(colour='red')
  28. def fun():
  29. dprint("this should be red")
  30.  
  31.  
  32. loggers = {'black': Logger("black"), 'yellow': Logger("yellow"), 'red': Logger("red")}
  33. dprint = loggers['black'].log
  34.  
  35. f()
  36. fu(5)
  37. fun()
Success #stdin #stdout 0.01s 9992KB
stdin
Standard input is empty
stdout
black: this should be black
yellow: this should be yellow, and 5 = 5
red: this should be red