| f | from functools import wraps | f | from functools import wraps |
| | | |
| def counter(func): | | def counter(func): |
| count = 0 | | count = 0 |
| | | |
| @wraps(func) | | @wraps(func) |
| n | def wrapper(*args, **kwargs): | n | def wrapped(*args, **kwargs): |
| nonlocal count | | nonlocal count |
| count += 1 | | count += 1 |
| return func(*args, **kwargs) | | return func(*args, **kwargs) |
| | | |
| def get_count(): | | def get_count(): |
| return count | | return count |
| t | wrapper.counter = get_count | t | wrapped.counter = get_count |
| return wrapper | | return wrapped |
| '\n@counter\ndef fun(a, b):\n return a * 1 + b\n\nprint(fun.counter() | | '\n@counter\ndef fun(a, b):\n return a * 1 + b\n\nprint(fun.counter())\ |
| )\nres = sum(fun(i, i + 1) for i in range(5))\nprint(fun.counter(), res) | | nres = sum(fun(i, i + 1) for i in range(5))\nprint(fun.counter(), res)\n |
| \n' | | ' |