for-in
loops, yield
from
and iterable unpacking only work with iterable objects. In order
to be iterable, an object should have either an __iter__
method or a __getitem__
method implementing the Sequence protocol.
When trying to iterate over an object which does not implement the required methods, a TypeError
will be raised.
Below is an example of a basic implementation of a iterator with __iter__
:
class IterExample(object):
def __init__(self):
self._values = [1,2,3,4]
def __iter__(self):
return iter(self._values)
Here is a similar example with __getitem__
:
class GetItemExample(object):
def __init__(self):
self._values = [1,2,3,4]
def __getitem__(self, item):
return self._values[item]
These implementations make it possible to execute the following program:
my_iterator = IterExample()
for i in my_iterator:
print(i) # Prints 1,2,3,4
my_iterator = GetItemExample()
for i in my_iterator:
print(i) # Prints 1,2,3,4
Note also that iterating over an asynchronous iterable, i.e. an
object having the __aiter__
method, requires the use of async for ... in
instead of for ...
in
. Failing to provide the async
keyword will result in a TypeError
stating the object is not iterable.