Last active
August 29, 2015 14:07
-
-
Save cutwater/04f9e3125857eca11029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect question!