2009-07-25
When code in generators runs
Due to another entry I'm in the process of writing, I was suddenly struck with a question: in Python, when does the code in a generator function start running?
First, the brief version of generators and iterators. To handwave
somewhat, a generator is a function that uses yield
to create and
return its results one at a time. Behind the scenes, such a function
actually returns an iterator object, which the Python interpreter uses
to freeze and unfreeze the actual code as the function's code calls
yield
and outside code asks for the next value.
There are two plausible answers to the question. First, the generator
function could run the actual function code up until the first time
it called yield
and then freeze it, create the iterator object, and
return. Second, the generator function could immediately create the
iterator object and not run any of your function code until someone
asked for the first value.
(You might think that the first answer is crazy, but its advantage is that it makes calling generator functions act normally for as long as possible; their code runs until they do something special.)
The answer is that no code in generator functions runs until someone
asks for the first value. In the extreme case, where the result of
calling the function is just discarded, that means that no code in your
generator is run at all. Note that this includes code in finally:
statements, which is what you'd expect since the flow of control never
reached the try:
/finally:
block to start with.
(This doesn't seem to be explicitly stated in the Python language reference, but it is explicit in PEP 255, which the language reference points to. I suspect that PEPs are considered more or less officially part of the language reference, and so this is guaranteed behavior.)
If you actually need generators (okay, iterators) that are always
finalized, I believe that you can't use yield
and will instead have to
build iterator objects by hand that have __del__
methods.