How to recognize a generator function?

Neil Schemenauer nas-usenet at arctrix.com
Wed Jun 18 10:00:57 EDT 2003


Oscar Rambla <tecnic at codidoc.com> wrote:
> I'm not been able to find a solution to the following:
> 
> How to recognize if a function is a generator function before calling it?
> (Alternatives to having to inspect code for a yield or wrapping it into a 
> class).

There is no way to do that.  Think about:

    def g():
        yield 1

    def f():
        return g()

also think about:

    def f():
        return iter([1])

There are lots of ways to return iterators in Python.
Generator-iterators are first class objects and be passed around.
What are you really trying to do?

  Neil




More information about the Python-list mailing list