Skip to content

make list_iterator able to be converted to bool #154845

Description

@ConnorTippets

Feature or enhancement

Proposal:

Currently, if you cast an empty list iterator (iter([])) to a bool, you get True

> bool(iter([]))
True

This is opposed to casting an empty list ([]) to a bool, where you get the much more reasonable result of False

> bool([])
False

I propose properly implementing boolean conversion for list_iterator, to prevent potentially confusing and unintuitive bugs.

I had a bit of a look under the hood, and I believe implementing tp_as_sequence in the _PyListIterObject class in /Include/internal/pycore_list.h on lines 69-73 would resolve this issue.

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions