wrapping yield ?

Michael Sparks Michael.Sparks at rd.bbc.co.uk
Thu Sep 12 12:18:01 EDT 2002


Hi,

I'd like to be able to wrap "yield"  in a function - is that
possible, and if not (my eading of stuff  I've seen so far
says not) are there any plans to make it possible?

Whilst it may seem odd, I'd like to be able to do this:

def my_generator(arg):
    dostuff....
    while(1):
       waitForSomethingCheckingPeriodically()
       processit

where

def waitForSomethingCheckingPeriodically():
   found = 0
   while(not found):
       x = checkTheThing()
       if foundRightThing(x):
           found = 1
      else:
         yield "foo"
   return x

The reason for wanting to wrap this up is due to the
waitForSomethingCheckingPeriodically is a sequence of
code that I'd like to have in several different generators.
The problem of course here is that "my_generator"
doesn't create a generator due to not containing a "yield"
keyword, whereas waitForSomethingCheckingPeriodically
generates a generator, even though I don't want it to be
one...

The context here is of a piece of network server code,
and wanting to be able to enforce relinquishing of control
in the pieces of code that perform reads/writes, as well as
connection acceptance etc. (basically I want to wrap up
the yield call into the piece of code that should yield control,
rather than rely on the user of the code being relied upon
to yield in the right places)

Having read: http://www.python.org/peps/pep-0255.html
I suspect that what I'd like to do here wouldn't work, and
what I really want is something more like:

def_generator my_generator(arg):
...
def waitForSomethingCheckingPeriodically():
...

And have the keyword (or something other than "yield")
indicate that the definition is a generator, since that would
solve this completely - which I not has been ruled out in that
document, and I'm hoping things may've changed since
then :-)

If this is the wrong place to ask this q - apologies, I'd
appreciate being pointed in the right direction :-)

Regards & thanks in advance,


Michael.






More information about the Python-list mailing list