Created
May 7, 2022 15:56
-
-
Save reinderien/ccd1acf45d5dabeca7a2a36bfcb46790 to your computer and use it in GitHub Desktop.
Generators close context managers
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 SomeCtx: | |
def __enter__(self): | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
print('I exited') | |
def some_generator(): | |
with SomeCtx(): | |
for i in range(10): | |
yield i | |
for i in some_generator(): | |
print(f'Consumed {i}') | |
if i >= 7: | |
raise ValueError() | |
print('The loop must have succeeded') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment