Help with coroutine-based state machines?
Neil Schemenauer
nas-usenet at arctrix.com
Fri May 30 01:33:56 EDT 2003
Alan Kennedy <alanmk at hotmail.com> wrote:
> Differentiating generator-functions from functions.
> ---------------------------------------------------
> But there isn't a generator type in the types module?
Is that a question? :-) Seriously, there is no generator type
because a generator-function has the same type as a function.
> So, that's a question I'd like to ask: can anyone tell me how
> to discover the generator-functions of an object, and
> differentiate them from ordinary functions?
Here's one way:
>>> def f():
... pass
...
>>> def g():
... yield 1
...
>>> f.func_code.co_flags & 0x20
0
>>> g.func_code.co_flags & 0x20
32
I don't recommend using it though. The CO_GENERATOR (0x20)
constant is not available from Python code, AFAIK.
One final parting shot. From the caller's point of view, there
is essentially no difference between:
def f():
return iter([1, 2, 3])
and:
def g():
for i in [1, 2, 3]:
yield i
or even:
def f2():
return g()
Cheers,
Neil
More information about the Python-list
mailing list