Skip to content

Instantly share code, notes, and snippets.

@cutwater
Last active August 29, 2015 14:07
Show Gist options
  • Save cutwater/04f9e3125857eca11029 to your computer and use it in GitHub Desktop.
Save cutwater/04f9e3125857eca11029 to your computer and use it in GitHub Desktop.
class gen():
def __enter__(self):
print('enter')
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit')
def test():
with gen():
for i in range(10):
yield i
t = test()
print(next(t))
print(next(t))
del t
print('after del')
import contextlib
@contextlib.contextmanager
def gen():
print('enter')
yield
print('exit')
def test():
with gen():
for i in range(10):
yield i
t = test()
print(next(t))
print(next(t))
del t
print('after del')
@surabujin
Copy link

Perfect question!

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