473,385 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How does a generator object refer to itself?

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
4 1370
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike | last post by:
I need to be able to store a list of schedulable items, either regular callables or generator-producing functions -- is there a way to test if a fn is generator-producing? (In the non-generator...
13
by: Emmanuel | last post by:
Hi, I run across this problem, and couldn't find any solution (python 2.2.2) : Code : =========== from __future__ import generators >>> class titi:
6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
7
by: Philip | last post by:
Hi all, Been having a really tricky problem. I'm trying to do some object oriented programming with javascript and it was all working fine until I had to support Netscape 4. The problem is...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
14
by: castironpi | last post by:
I'm actually curious if there's a way to write a generator function (not a generator expression) in C, or what the simplest way to do it is... besides link the Python run-time.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.