Skip to content

Instantly share code, notes, and snippets.

@ojas
Created August 9, 2013 21:34
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 ojas/6197437 to your computer and use it in GitHub Desktop.
Save ojas/6197437 to your computer and use it in GitHub Desktop.
with-blocks in python
class MyClass():
def __init__(self, *args, **kwargs):
print '>> init'
self.label = kwargs.get('label')
def __enter__(self):
print ' >> enter'
return self
def __exit__(self, type, value, traceback):
print ' << exit'
def dump(self):
print self.label
mc = MyClass(label = 'normal')
mc.dump()
with MyClass(label = 'using with') as mc:
mc.dump()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment