Skip to content

Instantly share code, notes, and snippets.

@rodrigogiraoserrao
Created September 3, 2021 13:31
Show Gist options
  • Save rodrigogiraoserrao/01eefefcab4f25ed5ff60661b08272dd to your computer and use it in GitHub Desktop.
Save rodrigogiraoserrao/01eefefcab4f25ed5ff60661b08272dd to your computer and use it in GitHub Desktop.
A simple Python🐍 3.8+ `dict` subclass that prints a debug message before setting/getting values.
class LogDict(dict):
def __setitem__(self, key, value):
print(f"Setting {key = } with {value = }")
return super().__setitem__(key, value)
def __getitem__(self, key):
print(f"Getting {key = }")
return super().__getitem__(key)
@rodrigogiraoserrao
Copy link
Author

I used this basic modification of the dict built-in in this tweet, when showing that consecutive assignments (like a = b = c = 73) happen from the left to the right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment