generator testing and attrs
Miki Tebeka
mikit at zoran.co.il
Thu Oct 9 20:11:45 EDT 2003
Hello Mike,
> 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?
if you do
>>> def f(): return 1
>>> dir(f)
and
>>> def f(): yield 1
dir(g)
You'll see that g has many attributes that f doesnt. Maybe you can use one of them.
> if isGeneratorFn(f):
> f = f()
> f.next() ...
> else:
> f()...
>
IMO a better way is letting the caller passing you ONLY functions.
In case of a generator if should pass the .next of the generator.
e.g.
l = []
def add_func(f):
l.append(f)
def add_gen(g):
l.append(g().next)
for f in l:
print f()
HTH.
Miki
More information about the Python-list
mailing list