Last active
August 12, 2021 10:47
-
-
Save erikbern/232099606372d853eabf18da16768868 to your computer and use it in GitHub Desktop.
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
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) | |
# Prints this: | |
# Assigning a to 42 | |
# Looking up a | |
# Assigning b to 1764 | |
# {'a': 42, 'b': 1764} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment