Skip to content

Instantly share code, notes, and snippets.

@erikbern
Last active November 5, 2021 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikbern/cbdc8150a96597b85d287bc7c6c63f68 to your computer and use it in GitHub Desktop.
Save erikbern/cbdc8150a96597b85d287bc7c6c63f68 to your computer and use it in GitHub Desktop.
Just a proof of concept of how you can inject your own "storage engine" for global variables
class MyDict(dict):
def __init__(self):
self._dict = {}
def __getitem__(self, k):
print(f'Looking up {k}')
return self._dict[k]
def __setitem__(self, k, v):
print(f'Assigning {k} to {v}')
self._dict[k] = v
locals = MyDict()
globals = MyDict()
exec('a = 42', globals, locals)
exec('b = a ** 2', globals, locals)
print(locals._dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment