Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Two argument form of iter() is not implemented. #5384

Closed
@hardkrash

Description

@hardkrash

The builtin iter(callable, sentinel) from pep 234 is not implemented.

The use case is something like this.

with open('file.bin', 'rb') as f:
    for chunk in iter(lambda: f.read(128), b''):
        process_chunk(chunk)

The two parameter form allows an easy way to make a callable into an iterable by using a sentinel.

It allows for replacing the do while workarounds with a simple iterator.

with open('file.bin', 'rb') as f:
    chunk = f.read(128)
    while chunk != b'':
        process_chunk(chunk)
        chunk = f.read(128)

Credit to this stack overflow answer from jfs
https://stackoverflow.com/a/20014805

Metadata

Metadata

Assignees

No one assigned

    Labels

    docspy-coreRelates to py/ directory in source

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions