473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1374
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(f unc): .... 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__.conte xt 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
1892
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 case, the fn is a 'tick' fn that I will call to produce results - a pre-generator implementation.) I can test for a generator itself with isinstance(f,...
13
2012
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
2734
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 = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
7
3502
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 that my object doesn't seem to be able to refer back to itself. Up until now, I've been using code like this... function Object { var Self = this;
6
22496
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was...
9
2164
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 usercontrol and all i'm really needing to do is get the selected value when the button is clicked on the form. Can someone tell what I need to...
4
1858
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 browsers) ? What about the new syntax of creating a object using { 'propName1':'propValue1', 'propName2':'propValue2', 'propName3':{ /* another object...
14
4821
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 can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the...
14
1206
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
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.