When code in generators runsDue 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 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 (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 (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 |
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |