Skip to content

Instantly share code, notes, and snippets.

@akesterson
Created November 18, 2014 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akesterson/e4cc27127c1374850f83 to your computer and use it in GitHub Desktop.
Save akesterson/e4cc27127c1374850f83 to your computer and use it in GitHub Desktop.
>>> def wat():
... raise Exception("ARGH")
... yield 0
... yield 1
... yield 2
...
>>> wat()
<generator object wat at 0x2e62500>
>>> for item in wat():
... print item
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in wat
Exception: ARGH
@akesterson
Copy link
Author

freenode #python set me straight on this one

[13:50] dash: akesterson: mmm, nope, exceptions work like normal
[13:50] akesterson: see the gist, no they don't. Not in 2.6 and 2.76
[13:50] akesterson: s/2.76/2.7/
[13:50] dash: akesterson: yes, that's exceptions working like normal
[13:50] dash: akesterson: as soon as the raise statement is run, the exception is raised
[13:51] dash: akesterson: calling a generator function creates a generator object but doesn't run anything in the function body
[13:51] dash: akesterson: that happens when the generator is iterated over
[13:51] akesterson: dash: I see
[13:51] akesterson: that makes more sense and enrages me less
[13:51] dash: akesterson: so, 'wat()' does not raise an exception because the raise statement hasn't been run yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment