![]()
ITUNES PODCAST URL EXTRACTOR CODEcalling list on it) is doing roughly the same thing as the for loop, ignoring the StopIteration in the same way nothing except code that specifically expects generators (as opposed to more generalized iterables and iterators) will ever bother to inspect the StopIteration object (at the C layer, there are optimizations that StopIteration objects aren't even produced by most iterators they return NULL and leave the set exception empty, which all iterator protocol using things know is equivalent to returning NULL and setting a StopIteration object, so for anything but a generator, there isn't even an exception to inspect much of the time). ITUNES PODCAST URL EXTRACTOR GENERATORAnything else that consumes the generator (e.g. ![]() And for anything that's not a generator, the StopIteration never has any associated values the for loop has no way to report them even if it did (it has to end the loop when it's told iteration is over, and the arguments to StopIteration are explicitly not part of the values iterated anyway). Don't use yield, and return works as expected (because it's not a generator function).Īs an example to explain what happens to the return value in normal looping constructs, this is what for x in gen(): effectively expands to a C optimized version of:Īs you can see, the expanded form of the for loop has to look for a StopIteration to indicate the loop is over, but it doesn't use it.No matter what you do, calling the generator function "returns" a new generator object (which you can loop over until exhausted), it can never return a raw value computed inside the generator function (which doesn't even begin running until you loop over it at least once). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |