How to recognise "generator functions" ?

I

imho

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.
 
G

Georg Brandl

imho said:
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 ?
99

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

Georg
 
I

imho

Georg Brandl ha scritto:
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! :)
 
J

John J. Lee

imho said:
Georg Brandl ha scritto:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top