Created
April 8, 2022 16:05
-
-
Save laixintao/6c7e697ee293b4bf7ba82e0eb0f8ac3e to your computer and use it in GitHub Desktop.
You can move put try-catch outside of the generator!
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 time | |
def generate(): | |
try: | |
for i in range(5): | |
time.sleep(0.5) | |
yield i | |
finally: | |
print("My jos is done, clock out!") | |
gen = generate() | |
for x in gen: | |
print(x) |
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 time | |
def generate(): | |
for i in range(5): | |
time.sleep(0.5) | |
yield i | |
try: | |
gen = generate() | |
finally: | |
print("My jos is done, clock out!") | |
for x in gen: | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
They are different!
stdout: