A Common Misunderstanding About #python Generators
▻https://hackernoon.com/a-common-misunderstanding-about-python-generators-dbc622914d33?source=rs
I received the following email a few days ago:Jeff,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 print() call: <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, (...)