Skip to content

Instantly share code, notes, and snippets.

@kekssw
Created March 17, 2010 07:34
Show Gist options
  • Save kekssw/335000 to your computer and use it in GitHub Desktop.
Save kekssw/335000 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
@contextmanager
def mark_enabling(value):
global mark
try:
mark = True
yield value
finally:
mark = False
def test():
global mark
print 'Yes' if mark else 'No'
test()
# No
with mark_enabling('Salsa') as v:
test()
print 'Value:', v
# Yes
# Value: Salsa
test()
# No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment