How to recognise "generator functions" ?

Discussion in 'Python' started by imho, Jul 19, 2006.

  1. imho

    imho Guest

    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.
     
    imho, Jul 19, 2006
    #1
    1. Advertising

  2. imho

    Georg Brandl Guest

    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
     
    Georg Brandl, Jul 19, 2006
    #2
    1. Advertising

  3. imho

    imho Guest

    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! :)
     
    imho, Jul 19, 2006
    #3
  4. imho

    John J. Lee Guest

    imho <> writes:

    > 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
     
    John J. Lee, Jul 22, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    332
    Patrice
    Jan 17, 2006
  2. Glenn McCall
    Replies:
    1
    Views:
    6,133
    Michael Black
    Feb 20, 2005
  3. RayKnight
    Replies:
    6
    Views:
    434
    Virgil Green
    Mar 28, 2005
  4. WindAndWaves
    Replies:
    1
    Views:
    413
    WindAndWaves
    Dec 15, 2004
  5. Terry Reedy

    Generator functions subclass generator?

    Terry Reedy, Jun 18, 2009, in forum: Python
    Replies:
    0
    Views:
    501
    Terry Reedy
    Jun 18, 2009
Loading...

Share This Page