fork download
  1. from functools import wraps
  2. def counter(f):
  3. count=0
  4. @wraps(f)
  5. def wrapper(*args):
  6. global count
  7. count=count+1
  8. return f(*args)
  9. def counter():
  10. return count
  11. f.counter=counter
  12. return wrapper
  13.  
  14.  
  15.  
  16.  
  17.  
  18. @counter
  19. def fun(a, b):
  20. return a * 1 + b
  21.  
  22. print(fun.counter())
  23. res = sum(fun(i, i + 1) for i in range(5))
  24. print(fun.counter(), res)
Runtime error #stdin #stdout #stderr 0.14s 23216KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 22, in <module>
AttributeError: 'function' object has no attribute 'counter'