Skip to content

Instantly share code, notes, and snippets.

@jcaxmacher
Last active December 11, 2015 22:18
Show Gist options
  • Save jcaxmacher/4668416 to your computer and use it in GitHub Desktop.
Save jcaxmacher/4668416 to your computer and use it in GitHub Desktop.
scope creep
def inject(**vars):
def decorator(fn):
def fun(*args, **kwargs):
gls = globals().copy()
gls.update(vars)
new_fn = type(fn)(fn.__code__, gls)
return new_fn(*args, **kwargs)
return fun
return decorator
@inject(q=10, z=11)
def test():
return q + z
@jcaxmacher
Copy link
Author

I'm not sure why any sane person would ever do this. But it is fun to know you can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment