I received the following email a few days ago:
It seems that you know about iterators. Maybe you can explain some weird behavior. If you run the code below you will find that the function is treated differently just because it has a 'yield' in it somewhere, even if it's completely unreachable.
def func(): print("> Why doesn't this line print?") exit() # Within this function, nothing should matter after this point. The program should exit yield "> The exit line above will exit ONLY if you comment out this line." x = func() print(x)
When I run the code, I get the following output from the
<generator object func at 0x10e968a50>.
So what's going on here? Why doesn't that line in
func() print? Even if
yield is completely unreachable, it seems to affect the way the function executes.