Skip to content

Instantly share code, notes, and snippets.

@ammaraskar
Created April 28, 2021 02:25
Show Gist options
  • Save ammaraskar/f77fcee78b3a86b46d98f3ecf8caf166 to your computer and use it in GitHub Desktop.
Save ammaraskar/f77fcee78b3a86b46d98f3ecf8caf166 to your computer and use it in GitHub Desktop.
>>> 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