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 exityield "> 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 call: . print() <generator object func at 0x10e968a50> So what’s going on here? Why that line in print? Even if is completely unreachable, it to affect the way the function executes. doesn’t func() yield seems How affects a function yield To shed some light on why this behavior is occurring, let’s review . Any function that includes the keyword is automatically converted to a . What it returns (the ) is a . Our print output is actually hinting at this: yield yield generator generator generator iterator $ python yield.py <generator object func at 0x10e968a50> When is executed, . Rather, since is a , a is returned. So while that may look like a function call, it's actually giving us the we would use to generate values yielded by the . x = func() we are not actually executing any of the code within _func()_ func() generator generator iterator generator iterator generator So how do we actually “call” a ? . In the code above, this would execute the "next" call to the returned by and bound to . generator By calling _next()_ on a generator iterator generator iterator func() x If we want to see that cryptic message actually printed out, simply change the last line of the code to . print(next(x)) Of course, calling over and over on something that's meant to be treated as an iterator is a bit cumbersome. Luckily, loops support iteration over . Imagine a toy generator implemented as follows: next() for generator iterators def one_to_ten():"""Return the integers between one and ten, inclusive."""value = 1while value <= 10:yield value value += 1 We can call this in a for loop in the following way: for element in one_to_ten():print(element) Of course, we could have more verbosely written: iterator = one_to_ten()for element in iterator:print(element) This is similar to what the original code did. It just never used to actually execute the code in the . x generator Summary I hope that clears up some common questions about and in . For a more in-depth tutorial on the topic, check out . yield generators Python Improve Your Python: 'yield' and Generators Explained Posted on Jun 04, 2018 by Jeff Knupp Originally published at jeffknupp.com on June 4, 2018.