isinstance(foo, types.GeneratorType) or inspect.isgenerator(foo)?
Question: It seems that there are two ways in Python to test whether an object is a
generator:

    
    
    import types
    isinstance(foo, types.GeneratorType)
    

or:

    
    
    import inspect
    inspect.isgenerator(foo)
    

In the spirit of "There should be one-- and preferably only one --obvious way
to do it.", is one of these ways recommended over the other (presumably they
do the same thing...if not, please enlighten me!)?


Answer: They are 100% equivalent:

    
    
    >>> print(inspect.getsource(inspect.isgenerator))
    def isgenerator(object):
        """Return true if the object is a generator.
    
        Generator objects provide these attributes:
            __iter__        defined to support interation over container
            close           raises a new GeneratorExit exception inside the
                            generator to terminate the iteration
            gi_code         code object
            gi_frame        frame object or possibly None once the generator has
                            been exhausted
            gi_running      set to 1 when generator is executing, 0 otherwise
            next            return the next item from the container
            send            resumes the generator and "sends" a value that becomes
                            the result of the current yield-expression
            throw           used to raise an exception inside the generator"""
        return isinstance(object, types.GeneratorType)
    

I'd say that using `isinstance(object, types.GeneratorType)` should be the
preferred way since it's clearer and simpler. Also `inspect.isgenerator` was
only added in python2.6, which means that using `isinstance` is more backward
compatible.

They probably added the `isgenerator` function for symmetry
[`isgeneratorfunction`](http://docs.python.org/2/library/inspect.html#inspect.isgeneratorfunction)
which does something different.

