python gripes survey

sismex01 at hebmex.com sismex01 at hebmex.com
Tue Aug 26 09:42:50 EDT 2003


[Gustavo Cordova]
>  > Generators yielding to other generators, to form a
>  > quasi-cooperative-multitasking environ seems quite
>  > powerful to me.

[Ryan Lowe]
>  not that i have any clue what you just said, but is this 
>  possible in python?
>  or only in code-block languages?

Nope, it's quite possible, and not too complicated to
do (but comprehend) in Python.

You just need a bunch of routines which need to run
in an "interleaved" fashion.  You put "yield" statements
where you intend to pause execution of each routine,
the secret is in which value you yield.

One approach is to put all obtained generators into a
list, and simply iterate through it repeatedly, taking
each one and calling it's .next()  You can use a sentinel
value to signal that you should take this generator off
the list.  The loop would look something like:

  while 1:
     for I in GeneratorsList:
        I.next()

The next approach is to yield in each iterator the reference
to the next subroutine to be called.  This is especially
neat in state machines, because each state *knows* which
other state follows given a certain condition.  So, you
replace the generator-list-iteration loop given above for
something like below:

  state = InitialStateGenerator
  while state: 
     state = state.next()

and this will loop until a certain state returns a false
value. Neat.

The final approach is for a generator to yield TO the final
state;  in this case you don't yield a reference to the next
generator, but you directly call it's .next() method, which
directly continues it's execution, etc.  So, the loop actually
disappears and becomes

   state = InitialStateGenerator
   state.next()

and by the time it returns, it's done.

:-)

NEAT.

-gustavo


Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.





More information about the Python-list mailing list