By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,445 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,445 IT Pros & Developers. It's quick & easy.

How to recognise "generator functions" ?

P: n/a
Hi all.

Is there a way to know if a function object is actually a "generator
function" or not ? e.g.:

def f():
pass

def g():
yield None

f.__class__ is the same as g.__class__ , i.e. "function" type.
But i "know" that the second, when invoked, returns a generator object,
because there is a "yield" statement in its body.

Is there a (eventually hackish) method to get such information ?

Thanks in advance,

Diego.
Jul 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
imho wrote:
Hi all.

Is there a way to know if a function object is actually a "generator
function" or not ? e.g.:

def f():
pass

def g():
yield None

f.__class__ is the same as g.__class__ , i.e. "function" type.
But i "know" that the second, when invoked, returns a generator object,
because there is a "yield" statement in its body.

Is there a (eventually hackish) method to get such information ?
>>f.func_code.co_flags
67
>>g.func_code.co_flags
99

=32 (CO_GENERATOR in compiler.consts) is the flag that indicates a
generator code object.

Georg
Jul 19 '06 #2

P: n/a
Georg Brandl ha scritto:
>>>f.func_code.co_flags
67
>>>g.func_code.co_flags
99

=32 (CO_GENERATOR in compiler.consts) is the flag that indicates a
generator code object.

Georg
What a fast reply!
Thank You very much! :-)
Jul 19 '06 #3

P: n/a
imho <ce***@comeno.itwrites:
Georg Brandl ha scritto:
>>f.func_code.co_flags
67
>>g.func_code.co_flags
99
=32 (CO_GENERATOR in compiler.consts) is the flag that indicates a
generator code object.
Georg

What a fast reply!
Thank You very much! :-)
Georg, that was cruel <0.2 wink>

Seriously, does anybody have any sensible reason to *use* this
information? Maybe some dynamic code-analysis tool or something?
John
Jul 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.