Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rahulremanan/dbc8caca6f60eaea7b20a4a69fb51cba to your computer and use it in GitHub Desktop.
Save rahulremanan/dbc8caca6f60eaea7b20a4a69fb51cba to your computer and use it in GitHub Desktop.
A simple Python generator function
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