[Python-Dev] feature request: inspect.isgenerator

Georg Brandl g.brandl at gmx.net
Mon May 29 22:09:21 CEST 2006


Fredrik Lundh wrote:
> Terry Reedy wrote:
> 
>>> def isgenerator(func):
>>>    return func.func_code.co_flags == 99
>>>
>>> but it is rather ugly (horrible indeed).
>> 
>> To me, part of the beauty of Python is that is so easy to write such things 
>> so compactly, so that we do not need megaAPIs with hundreds of functions 
>> and methods.
> 
> so what's so "easy" about the magic constant 99 ?
> 
> is there some natural and obvious connection between generators and that 
> number that I'm missing, or is that constant perhaps best hidden inside 
> some introspection support module?

It seems to be a combination of CO_NOFREE, CO_GENERATOR, CO_OPTIMIZED and
CO_NEWLOCALS.

The first four CO_ constants are already in inspect.py, the newer ones
(like CO_GENERATOR) aren't.

I wonder whether a check shouldn't just return (co_flags & 0x20), which
is CO_GENERATOR.

Georg



More information about the Python-Dev mailing list