Skip to content

range(2,3) in range(1,5) should return True (sub-sequence test) or raise TypeError. #141005

@simonltwick

Description

@simonltwick

Feature or enhancement

Proposal:

This came up as I was looking for overlapping ranges.

The expression range(2,3) in range(1,5) is accepted as valid Python but always returns false.

I was expecting it to return True. However I see that from the documentation the strict definition is "True if it's equal to an element in the range, else False", so this is working as per the documentation, although it is confusing.

I see that note 1 in the documentation ([(https://docs.python.org/3/library/stdtypes.html#typesseq)]) states that some specialised elements (string, bytes, buffer) have implemented subsequence testing, e.g. "gg" in "eggs".

I'm hoping that range can have this implemented so that range(2,3) in range(1,5) returns True.
If not, perhaps it should raise a TypeError or ValueError to prevent that type of operation?

Thank you for considering this suggestion, and acknowledgement to Stefano Rivera who suggested I submit this as an upstream request.
Simon Lambourn.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions