generator testing and attrs
Mike
vimakefile at yahoo.com
Thu Oct 9 12:04:28 EDT 2003
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, types.GeneratorType),
but I'm interested in the fn that produces it. For example:
if isGeneratorFn(f):
f = f()
f.next() ...
else:
f()...
If not, I guess it's fairly easy to specify a fn attr like so:
def myGen(): yield foo
myGen.isGenerator = True
but this is not as easy for instance methods, it seems. (No?)
(BTW, +1 from me for PEPs with syntax that let you specify attrs for fn's
and methods in-line with the def'n for cases like this, 'staticmethod', &c.
No reason that c# should win on this one...)
I would call f() the first time and see if the result is a generator object,
but unfortunately in my case returning a generator is possible valid 'data'
(to be scheduled later) and doesn't mean I should call .next() straight
away.
Ideas?
thanks
More information about the Python-list
mailing list