How to recognise "generator functions" ?

Georg Brandl g.brandl-nospam at gmx.net
Wed Jul 19 18:10:02 EDT 2006


imho wrote:
> Hi all.
> 
> Is there a way to know if a function object is actually a "generator 
> function" or not ? e.g.:
> 
> def f():
> 	pass
> 
> def g():
> 	yield None
> 
> f.__class__ is the same as g.__class__ , i.e. "function" type.
> But i "know" that the second, when invoked, returns a generator object, 
> because there is a "yield" statement in its body.
> 
> Is there a (eventually hackish) method to get such information ?

>>> f.func_code.co_flags
67
>>> g.func_code.co_flags
99

=> 32 (CO_GENERATOR in compiler.consts) is the flag that indicates a
generator code object.

Georg



More information about the Python-list mailing list