How can I check if an object is an iterator in Python?

前端 未结 8 1905
天命终不由人
天命终不由人 2020-12-25 10:10

I can check for a next() method, but is that enough? Is there an ideomatic way?

8条回答
  •  渐次进展
    2020-12-25 11:13

    This example comes from the book Effective Python and is illustrated in this post.

    An iterable produces an iterator. Any iterator is also an iterable, but produces itself as the iterator:

    >>> list_iter = iter([])
    >>> iter(list_iter) is list_iter
    True
    

提交回复
热议问题