What makes something iterable in python

后端 未结 4 753
既然无缘
既然无缘 2020-12-02 11:01

What makes something iterable in Python? ie. can loop over it with for

Is it possible for me to create an iterable class in Python? If so, how?

4条回答
  •  佛祖请我去吃肉
    2020-12-02 11:50

    Any object with an __iter__() method is considered an iterable.

    Additionally, any sequence (objects that have an __getitem__() method) could return an iterator. An iterator is an object with a __next__() method that returns the next object in the sequence and throws a StopIteration exception.

提交回复
热议问题