isgenerator(...) - anywhere to be found?
Diez B. Roggisch
deets at nospam.web.de
Tue Jan 22 09:17:14 EST 2008
Stefan Rank wrote:
> on 22.01.2008 14:20 Diez B. Roggisch said the following:
>>
>> def isgenerator(v):
>> def _g(): yield
>> return type(v) == type(_g())
>>
>> But I wonder why there is no such method already available?
>
>
> This tests for generator objects, and you could also use::
>
> return type(v) is types.GeneratorType
>
> I think that this is pretty direct already.
Not as nice as it could be, but certainly way less hackish than my approach.
Thanks!
> 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)
Not sure if that's not a bit too much on the dark magic side.. but good to
know that it exists.
Diez
More information about the Python-list
mailing list