How to tell the difference between an iterator and an iterable?

前端 未结 4 634
醉梦人生
醉梦人生 2021-02-01 08:13

In Python the interface of an iterable is a subset of the iterator interface. This has the advantage that in many cases they can be treated in the same way. However, there is an

4条回答
  •  轮回少年
    2021-02-01 08:50

    Because of Python's duck typing,

    Any object is iterable if it defines the next() and __iter__() method returns itself.

    If the object itself doesnt have the next() method, the __iter__() can return any object, that has a next() method

    You could refer this question to see Iterability in Python

提交回复
热议问题