By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

How does a generator object refer to itself?

P: n/a
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!

May 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Russell wrote:
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!

def self_decorate(func): .... class context:
.... def __init__(self):
.... self.me = None
.... def start(self):
.... self.me = func(self)
.... return self.me
.... return context().start
.... @self_decorate .... def me(self):
.... print "ME!",self.me
.... yield 1
.... me <bound method context.start of <__main__.context instance at 0x402e940c>> x=me()
x <generator object at 0x402e94ac> x.next()

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.

May 25 '06 #2

P: n/a
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

May 25 '06 #3

P: n/a
Michael wrote:
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.

May 26 '06 #4

P: n/a
> 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. :-)

May 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.