isgenerator(...) - anywhere to be found?
Paul McGuire
ptmcg at austin.rr.com
Tue Jan 22 10:09:05 EST 2008
On Jan 22, 7:46 am, Stefan Rank <list-e... at strank.info> wrote:
> I also need to test for generator functions from time to time for which
> I use::
>
> def _isaGeneratorFunction(func):
> '''Check the bitmask of `func` for the magic generator flag.'''
> return bool(func.func_code.co_flags & CO_GENERATOR)
>
> cheers,
> stefan
Might want to catch AttributeError in this routine - not all func
arguments will have a func_code attribute. See below:
class Z(object):
def __call__(*args):
for i in range(3):
yield 1
for i in Z()():
print i
# prints 1 three times
import types
print type(Z()()) == types.GeneratorType
# prints 'True'
print Z()().func_code
# raises AttributeError, doesn't have a func_code attribute
-- Paul
More information about the Python-list
mailing list