Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created August 3, 2022 13:10
Show Gist options
  • Save tebeka/a8d385fe06b9789c1c790329f1c4fb74 to your computer and use it in GitHub Desktop.
Save tebeka/a8d385fe06b9789c1c790329f1c4fb74 to your computer and use it in GitHub Desktop.
from datetime import datetime
class LoggingProxy:
"""Log attribute access to proxied object."""
def __init__(self, obj):
self._obj = obj
def __getattr__(self, attr):
val = getattr(self._obj, attr, None)
if val is None:
raise AttributeError(attr)
print(f'[DEBUG] {attr} -> {val!r}')
return val
now = LoggingProxy(datetime.now())
print(now.year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment