Created
March 31, 2023 15:40
-
-
Save rahulremanan/dbc8caca6f60eaea7b20a4a69fb51cba to your computer and use it in GitHub Desktop.
A simple Python generator function
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
def f(): | |
'''Modified PEP 255 example''' | |
try: | |
yield 1 | |
try: | |
yield 2 | |
1/0 # zero division to trigger ZeroDivisionError | |
yield 3 # never get here | |
except ZeroDivisionError: | |
yield 4 | |
yield 5 | |
raise # raise exception | |
except: | |
yield 6 # never get here | |
yield 7 # the "raise" above stops this | |
except Exception as e: | |
# print(e) | |
yield 8 | |
finally: | |
yield 9 # always execute | |
try: | |
x = 12 | |
finally: | |
yield 10 # always execute | |
yield 11 | |
print (list(f())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment