PEP 255: Simple Generators, Revised Posting
Tim Peters
tim at digicool.com
Mon Jun 25 15:02:22 EDT 2001
def emptyg():
return
yield None # never reached
[Bernhard Herzog]
> Only until the compiler becomes smart enough to remove
> that unreachable yield statement. :)
[Rainer Deyke]
> By that time, the compiler will hopefully be smart enough to
> perform static analysis *before* removing dead code.
[Skip Montanaro]
> My peephole optimizer would have removed it...
That's OK: the yield has no *runtime* purpose there, it's only being used
as a signal to the compiler to set the CO_GENERATOR flag in the code
object's co_code member.
Under the covers, generator code objects and bytecode are exactly the same
as for non-generators, except for that flag bit. So code that "looks dead"
is just as dead as before <wink>. There's nothing magic about RETURN_VALUE
in a generator's bytecode either. In fact, eval_frame() knows nothing about
generators (it never looks at the CO_GENERATOR flag).
More information about the Python-list
mailing list