Skip to content

Instantly share code, notes, and snippets.

@bobpoekert
Created August 22, 2012 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobpoekert/3427675 to your computer and use it in GitHub Desktop.
Save bobpoekert/3427675 to your computer and use it in GitHub Desktop.
(binding) in python
from contextlib import contextmanager
@contextmanager
def binding(**pairs):
g = globals()
prev = dict((k, g[k]) for k in pairs.iterkeys() if k in g)
g.update(pairs)
try:
yield
finally:
g.update(prev)
""" ex:
>>> foo = False
>>> foo
False
>>> with binding(foo = True):
foo
True
>>> foo
False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment