Iterator
Last updated
Last updated
Iterator is an object representing a stream of data; this object returns the data one element at a time.
Iterator must support a method called __next__()
that takes no arguments and always returns the next element of the stream. If there are no more elements in the stream, __next__()
must raise the StopIteration exception. Iterators don’t have to be finite, though; it’s perfectly reasonable to write an iterator that produces an infinite stream of data.
The built-in iter()
function takes an arbitrary object and tries to return an iterator that will return the object’s contents or elements, raising TypeError if the object doesn’t support iteration. Several of Python’s built-in data types support iteration, the most common being lists and dictionaries. An object is called iterable if you can get an iterator for it.
Iterator is:
Object with defined __next__()
(next()
for Python2), __iter__()
__next__()
takes no arguments and always returns the next element of the stream. If there are no more elements in the stream, __next__()
must raise the StopIteration
exception
iterator can be infinite
__iter__()
return self (to use inside for
loop which uses iter()
for it's data)
Object returned by generator function (the one that uses yield
)
Object returned by iter(<iterable>)
Generator is partial case of iterator.
__next__()
gives next value or raises StopIteration
exception
__iter__()
returns iterator itself (used by for
operator)
So, it's exactly how for
and iter
works! It just used object's __iter__()
method. In other case object must support the sequence protocol (the __getitem__()
method with integer arguments starting at 0
).
The best way to get all elements of iterator - it's to feed it to list()
🪄 Code:
📟 Output:
🪄 Code:
📟 Output: