Skip to content

Instantly share code, notes, and snippets.

@pimiento
Created March 15, 2023 08:50
Show Gist options
  • Save pimiento/726654551c4b38dc6cf5c306f8e9d0d4 to your computer and use it in GitHub Desktop.
Save pimiento/726654551c4b38dc6cf5c306f8e9d0d4 to your computer and use it in GitHub Desktop.
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