Created
April 28, 2021 02:25
-
-
Save ammaraskar/f77fcee78b3a86b46d98f3ecf8caf166 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 CustomGlobals(dict): | |
... def __getitem__(self, key): | |
... if key == 'x': | |
... return get_x() | |
... return super().__getitem__(key) | |
... | |
>>> def get_x(): | |
... print('get_x called!') | |
... return 1+1 | |
... | |
>>> eval('print("x + 1 =", x + 1)', CustomGlobals()) | |
get_x called! | |
x + 1 = 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment