Co-routines

Peter Hansen peter at engcorp.com
Thu Jul 17 11:06:17 EDT 2003


thewrights at ozemail.com.au wrote:
> 
> From the python grammar:
> 
>     funcdef    ::= "def" funcname "(" [parameter_list] ")" ":" suite
> 
> I want the statements in <suite> to be executed on a statement by statement
> basis, with no particular restrictions on *what* those statements are. It's
> the execution stepping I'm interested in... the I/O in my example was just
> that. I understand that there will be all the problems on contention/race
> etc (just like if the functions were going in separate threads)

Okay, that's clear.  It can't be done.  :-)  Now on to the next step:
what are you actually trying to do.  That is, *why* do you want the 
behaviour you say you want?  That might give us a few ideas about what
can really be done.

Basically, what you describe would require the interpretation of 
Python at a higher level than it is interpreted now.  It is actually
*compiled* code, turned from source (the lines you refer to) into
bytecodes, which are effectively machine code for the Python virtual 
machine.  It is at the level of bytecodes that one might actually
start to get the control you're talking about.

Another approach would be if you reduced the problem from arbitrary
statements to some kind of subset.  Maybe evaluation of expressions
only, without loops or most statements?  Of course, that might be 
far too limited for you, but you haven't said why you want to do this
yet.  If you could take that approach, you would basically retrieve
the source line by line and pass it to "exec" or maybe eval().

You could also just define your own mini-language, and write an
interpreter for that which has the behaviour you need.

Or, depending again on why you want all this, you could require 
construction of the code using generators or something, as others
are suggesting.

-Peter




More information about the Python-list mailing list