[Baypiggies] introspecting for generator vs non-generator function
wesley chun
wescpy at gmail.com
Sat Nov 15 06:23:36 CET 2008
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:
http://www.python.org/doc/2.5.2/ref/types.html
look for the "Internal Types" -> "Code Objects" section where it reads
as follows:
"The following flag bits are defined for co_flags: bit 0x04 is set if
the function uses the "*arguments" syntax to accept an arbitrary
number of positional arguments; bit 0x08 is set if the function uses
the "**keywords" syntax to accept arbitrary keyword arguments; bit
0x20 is set if the function is a generator."
cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com
"Python Web Development with Django", Addison Wesley, (c) 2009
http://withdjango.com
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
More information about the Baypiggies
mailing list