Question about bytecode and yield

Michael Hudson mwh at python.net
Tue Jul 20 09:49:15 EDT 2004


"Robert Brewer" <fumanchu at amor.org> writes:

> [Daniel Brodie]
> > ...Is it possible to, at runtime, change the bytecode
> > of a function so that to turn it into a generator
> > from a normal, average, function?
> 
> [Tim Peters]
> > No.
> 
> But perhaps Mr. Brodie would be satisfied with creating a new function
> based on the old. Doesn't "compile-time" include new.code()?
> 
> [Daniel Brodie]
> > Would just putting in a YIELD_VALUE (in a sane way, 
> > obviously) work?
> 
> [Tim Peters]
> > No.
> 
> Perhaps setting the CO_ flags appropriately when using new.code() would
> be sufficient...? I've had good success tinkering with those myself
> (just not with generators yet). No time to test my theory at the moment,
> so I'm hoping someone can answer definitively.

I didn't manage to divine exactly what Daniel was after, but using
new.code() is almost certainly part of the solution...

Cheers,
mwh

-- 
 "Well, the old ones go Mmmmmbbbbzzzzttteeeeeep as they start up and
  the new ones go whupwhupwhupwhooopwhooooopwhooooooommmmmmmmmm."
                         -- Graham Reed explains subway engines on asr



More information about the Python-list mailing list