We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
11
766
yield is not as magical this answer suggests. When you call a function that contains a yield statement anywhere, you get a generator object, but no code runs. Then each time you extract an object from the generator, Python executes code in the function until it comes to a yield statement, then pauses and delivers the object. When you extract another object, Python resumes just after the yield and continues until it reaches another yield (often the same one, but one iteration later). This continues until the function runs off the end, at which point the generator is deemed exhausted.
"These iterables are handy... but you store all the values in memory and this is not always what you want", is either wrong or confusing. An iterable returns an iterator upon calling the iter() on the iterable, and an iterator doesn't always have to store its values in memory, depending on the implementation of the iter method, it can also generate values in the sequence on demand.
It would be nice to add to this great answer why It is just the same except you used () instead of [], specifically what () is (there may be confusion with a tuple).
@MatthiasFripp "This continues until the function runs off the end" -- or it encounters a return statement. (return is permitted in a function containing yield, provided that it does not specify a return value.)
The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list.
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
complete the sentence: my question is about...
use tags that describe things or concepts that are essential, not incidental to your question
yield
is not as magical this answer suggests. When you call a function that contains ayield
statement anywhere, you get a generator object, but no code runs. Then each time you extract an object from the generator, Python executes code in the function until it comes to ayield
statement, then pauses and delivers the object. When you extract another object, Python resumes just after theyield
and continues until it reaches anotheryield
(often the same one, but one iteration later). This continues until the function runs off the end, at which point the generator is deemed exhausted.()
instead of[]
, specifically what()
is (there may be confusion with a tuple).return
statement. (return
is permitted in a function containingyield
, provided that it does not specify a return value.)