ID EN
Built-in Functions

iter

Python 3.11

Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iterable protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to it

Syntax

PYTHON
iter(object)

Examples

Example 1
PYTHON
from functools import partial
with open('mydata.db', 'rb') as f:
    for block in iter(partial(f.read, 64), b''):
        process_block(block)

See Also

iterator iterable __iter__() __getitem__() TypeError __next__() StopIteration Iterator Types