How to find parent function/class of generators?

J. A. Aczel seventhhs at gmail.com
Sat Aug 25 04:19:53 EDT 2007


Hello all. I think I should explain my problem in detail, so everyone
understands what I'm trying to do and why. There may be a better
approach to the whole problem then I am using.

I am working on a game, with most of my AI code & such divided up into
generator functions (really coroutines that don't take any input)
stored in a dictionary called Director.tasks, and handled by a simple
cooperative multitasking engine. The dictionary key is the generator
object to be advanced, and the value is the frame number at which it
should next be executed. The generator objects yield the delay before
their next execution.

    def realtick(self):
        Director.frame += 1
        for task in Director.tasks.items():
            if task[1] <= Director.frame:
                try:
                    result = task[0].next()
                    if result == -1:
                        Director.tasks.pop(task[0])
                    else:
                        Director.tasks[task[0]] = Director.frame + result
                except:
                    Director.tasks.pop(task[0])

Why did I use a dictionary? Originally I had a list of lists, but I
thought converting it to a dictionary would make removing tasks from
the queue easier, the main difficulty I'm having. But it didn't,
because I can't identify the

Due to the framework I'm using, I think (or possibly just the Python
language?) in-game objects aren't actually destroyed when they're
removed from the game world. Thus, when an enemy or something with
associated tasks 'dies', the tasks merrily continue executing... and
the game will have a bunch of enemies who no longer have sprites,
movement abilities or collision checks, but otherwise are none the
worse for wear for having been destroyed. It's a big mess. So I need a
way to remove all tasks associated with a game object from the queue,
upon the object's  'destruction'.

Unfortunately, generator objects don't seem to include any information
about the parent object or function that created them. I ran the dir()
builtin on a sample generator object, created from an object method,
and got this:
['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close',
'gi_frame', 'gi_running', 'next', 'send', 'throw']

__class__ just contains <type 'generator'>. If __doc__'s the
docstring, I suppose I could use that to identify the generator
object, but it seems hackish.... nothing else in that list looks
likely. Is this python's message to me that I'm abusing generators for
entirely the wrong purpose? In any case, since I can't figure out how
to determine which generator object corresponds to which game object,
I'm stuck.

I'd appreciate any pointers or suggestions. :) I might have missed
something very obvious, since I'm no pythonista and have generally
been working on this in short spurts late at night. If so, sorry for
making you read all of this!



More information about the Python-list mailing list