Skip to content

Instantly share code, notes, and snippets.

@kozo2
Forked from ax3l/noglobal.py
Created May 30, 2018 15:57
Show Gist options
  • Save kozo2/a7b51fa0fbf740b8bb2f9452fc2398c2 to your computer and use it in GitHub Desktop.
Save kozo2/a7b51fa0fbf740b8bb2f9452fc2398c2 to your computer and use it in GitHub Desktop.
Useful Noglobal in Python
# License:
# I hereby state this snippet is below "threshold of originality" where applicable (public domain).
#
# Otherwise, since initially posted on Stackoverflow, use as:
# 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 = lambda fn: types.FunctionType(fn.__code__, dict(imports()))
# usage example
import numpy as np
import matplotlib.pyplot as plt
import h5py
a = 1
@noglobal
def f(b):
h5py.is_hdf5("a.tmp")
# only np. shall be known, not numpy.
np.arange(4)
#numpy.arange(4)
# this var access shall break when called
#print(a)
print(b)
f(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment