fork(1) download
  1. from functools import wraps
  2.  
  3. class SomeDecorator:
  4.  
  5. def __init__(self, value):
  6. print("Decorator init called")
  7. self.value = value
  8.  
  9. def __call__(self, func):
  10. @wraps(func)
  11. def inner(*args, **kwargs):
  12. print("decorator value: ", self.value)
  13. self.value += 10
  14. return func(*args, **kwargs)
  15. return inner
  16.  
  17. "____________________________________________________________"
  18.  
  19. @SomeDecorator(value=1)
  20. def f1(*args, **kwargs):
  21. return "f1"
  22.  
  23. @SomeDecorator(value=2)
  24. def f2(*args, **kwargs):
  25. return "f"
  26.  
  27. f1()
  28. f2()
  29. f1()
  30. f2()
Success #stdin #stdout 0.02s 9072KB
stdin
Standard input is empty
stdout
Decorator init called
Decorator init called
decorator value:  1
decorator value:  2
decorator value:  11
decorator value:  12