Created
March 15, 2023 08:50
-
-
Save pimiento/726654551c4b38dc6cf5c306f8e9d0d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
class CountCalls: | |
def __init__(self, x): | |
self.x = x | |
def __call__(self, func): | |
functools.update_wrapper(self, func) | |
self.func = func | |
self.num_calls = 0 | |
print(f"X == {self.x}") | |
def wrapper(*args, **kwargs): | |
self.func(*args, **kwargs) | |
self.num_calls += 1 | |
print(f"Call {self.num_calls} of {self.func.__name__!r}") | |
return wrapper | |
@CountCalls(123) | |
def say_whee(): | |
print(f"Whee!") | |
say_whee() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment