Skip to content

StopIteration from first() caught by a generator #465

Open
@mrkrd

Description

@mrkrd

Hello, I've noticed a surprising behavior of first() which can be well illustrated by:

print(list(map(
    first,
    [[1,2,3], [], [1,2,3], [1,2,3]]
)))

I would expect this statement to raise an exception, because first([]) normally raises an exception on an empty sequence. However, the exception is StopIteration which seems to interfere with map and stops its iteration.

What do you think about catching StopIteration in first() and re-raising a different exception, so it can propagate until explicitly caught? Which exception would be appropriate?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions