[Baypiggies] introspecting for generator vs non-generator function
Matt Good
matt at matt-good.net
Sat Nov 15 20:01:55 CET 2008
On Nov 14, 2008, at 9:23 PM, wesley chun wrote:
> On Fri, Nov 14, 2008 at 8:04 PM, Alex Martelli <aleaxit at gmail.com>
> wrote:
>> On Fri, Nov 14, 2008 at 7:44 PM, Matt Good <matt at matt-good.net>
>> wrote:
>>> There's a co_flags for that:
>>> "The following flag bits are defined for co_flags: ... bit 0x20 is
>>> set if
>>> the function is a generator."
>>>
>>> So:
>>> def is_generator(func):
>>> return bool(func.func_code.co_flags & 0x20)
>>
>> Yes it was -- I just approved it. Thanks! If there's one thing that
>> makes me even happier than teaching, it's learning!-)
>
>
> i have to agree with alex... that was a great find matt! for those
> interested in exploring further where he found it, it's in the 2.5.2
> docs:
Thanks. I'll admit that I didn't know that one off the top of my
head, so I just did a quick dump of attributes that differed between
the two:
for k in dir(f.func_code):
if getattr(f.func_code, k, None) != getattr(g.func_code, k, None):
print k
When I saw co_flags I remembered the bit about the generator flag from
looking into the varargs flags not too long ago.
HTH someone else digging into these esoteric bits of the language :)
-- Matt
More information about the Baypiggies
mailing list