Skip to content

Instantly share code, notes, and snippets.

@nyanpasu64
Created May 4, 2020 12:00
Show Gist options
  • Save nyanpasu64/7d00cd2a42224d90e3a45c9c4b55152a to your computer and use it in GitHub Desktop.
Save nyanpasu64/7d00cd2a42224d90e3a45c9c4b55152a to your computer and use it in GitHub Desktop.
# https://gist.github.com/ax3l/59d92c6e1edefcef85ac2540eb056da3
# CC-BY-SA 3.0 skyking, Glenn Maynard, Axel Huebl
# http://stackoverflow.com/a/31047259/2719194
# http://stackoverflow.com/a/4858123/2719194
import types
def imports():
for name, val in globals().items():
# module imports
if isinstance(val, types.ModuleType):
yield name, val
# functions / callables
if hasattr(val, '__call__'):
yield name, val
"""
@noglobal functions won't see global variables
unless you explicitly capture them.
This way you avoid accidentally using global data,
which I have done in the past.
"""
noglobal = lambda **keep: lambda fn: \
types.FunctionType(fn.__code__, dict(imports(), **keep))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment