Last active
November 5, 2021 12:32
-
-
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
This file contains 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
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