반응형

Iterator, Iterable, __iter__, __next__

반응형

Iterables are things that can be iterated, usually, these are some kind of container elements that contain items. Common examples are lists, tuples, or dictionaries.


In order to iterate an iterable, you use an iterator. An iterator is the object that helps you iterate through the container. For example, when iterating a list, the iterator essentially keeps track of which index you are currently at.


To get an iterator, the __iter__ method is called on the iterable. The is like a factory method that returns a new iterator This is like a factory method that returns a new iterator for this specific iterable. 


The iterator generally needs a single method, __next__, which returns the next item for the iteration.

반응형

이 글을 공유하기

댓글

Designed by JB FACTORY