def is_iterator(obj):
    """Detect if the object provided implements the iterator protocol.

    (i.e. like a generator).

    This will return False for objects which are iterable, but not
    iterators themselves.
    """
    from types import GeneratorType

    if isinstance(obj, GeneratorType):
        return True
    elif not hasattr(obj, '__iter__'):
        return False
    else:
        # Types which implement the protocol must return themselves when
        # invoking 'iter' upon them.
        return iter(obj) is obj
Detect if the object provided implements the iterator protocol.

    (i.e. like a generator).

    This will return False for objects which are iterable, but not
    iterators themselves.
    
cherrypy/lib/__init__.py