Skip to content

any() and generator behavior should be documented #121034

@anki-code

Description

@anki-code

When you're using generator and any() the rest of the code after first yielding True will be omitted.
I understand the logic behind it but it looks a bit like an issue or rake and should be documented in https://docs.python.org/3/library/functions.html#any at least.

def f():
    print(1, end=' ')
    yield False
    print(2, end=' ')
    yield True
    print(3, end=' ')

print(any(f()))
# 1 2 True

print(list(f()))
# 1 2 3 [False, True]

for i in f():
    print(i, end=' ')
# 1 False 2 True 3

Real life example of stepping on this rake: xonsh/xonsh#5539

CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux, macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions