How does a generator object refer to itself?

R

Russell

This is more a Python 2.5 question, since it is the send()
method that makes this so useful. The issue is how to write
a generator that refers to its own generator object. This
would be useful when passing control to some other function
or generator that is expected to return control via a send():

def me():
..
nextVal = yield you(me.send) # This is wrong!

That almost looks right, except that "me" isn't really the
generator object that is executing, it is the function that
produces the generator object. It seems somewhere I read
that some keyword ("generator"?) would work in this context,
but now I can't find where I read that. Maybe I imagined it.

Thanks!
 
M

Michael

Russell said:
The issue is how to write a generator that refers to its own generator
object.

def me():
..
nextVal = yield you(me.send) # This is wrong!
.... class context:
.... def __init__(self):
.... self.me = None
.... def start(self):
.... self.me = func(self)
.... return self.me
.... return context().start
........ def me(self):
.... print "ME!",self.me
.... yield 1
....ME! <generator object at 0x402e94ac>
1

You don't need python 2.5 at all to do this. You do need to have a token
mutable first argument though, as you can see. For more examples of how to
add context to generators in the interests of doing fun and interesting
things loosely coupled, please look at our MiniAxon tutorial here:
http://kamaelia.sf.net/MiniAxon/

(We don't tend to use decorators though because we'd like our code to run on
Nokia phones as well which use a variant of python 2.2)

For examples of fun things you can do:
http://kamaelia.sf.net/KamaeliaMacro.html
http://kamaelia.sf.net/Cookbook.html

Basic approach we're taking is a riff on the idea of Unix Philosophy:
Write components that do one thing and do it well.
Write components to work together.
Write components to handle object streams, because that is a universal
interface.

.... with apologies to Doug McIlroy.

:)


Michael.
 
M

Michele Simionato

Why don't you use a class ?

class MyGen(object):
def __iter__(self):
for i in range(2):
yield "I know who I am %s" % self

gen_obj = MyGen()

for x in gen_obj:
print x

For language lawyers: strictly speaking gen_obj is not a generator
object (since it is
restartable) but it may work for you anyway.


Michele Simionato
 
R

Russell

Michael said:
You don't need python 2.5 at all to do this. You do need to
have a token mutable first argument though, as you can see.

Thank you. That's a pattern similar to one we're using, where
a new object refers to the generator. The problem we're seeing
is that it seems to fool the garbage collector. We're not
positive about that. But we are suspicious.

Interesting page you have. I've bookmarked it. Thanks, again.
 
R

Russell

Why don't you use a class ?

Because we use this pattern for thousands of functions,
and don't want thousands of new classes. Right now
we use a single class that creates an instance for each
such generator. I was hoping to find a way to get even
more lightweight than that. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top