Created
December 12, 2024 15:19
-
-
Save frenata/f8944ec6260c49d372ee040519a1a779 to your computer and use it in GitHub Desktop.
abuse python decorators + default mutable args
This file contains hidden or 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 | |
import datetime as dt | |
def capture(): | |
def decorator(func): | |
@functools.wraps(func) | |
def wrapper(*args, ctx=[], **kwargs): | |
if len(ctx) > 0: | |
print(f"this function was last called at {ctx[-1][0]}") | |
ctx.append((dt.datetime.now(), args, kwargs)) | |
return func(*args, **kwargs) | |
return wrapper | |
return decorator | |
@capture() | |
def add(x, y): | |
return x+y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment