Implement Handling for Negative Range (l > r) in Python's range() Function

You can do this with an explicit step argument: range(30, 3, -1)

1 Like

The behavior is well defined. Changing it would be a breaking change that would break thousands, if not millions, of scripts.

2 Likes

Yes it does. It returns an empty range. So changing this would be a significant backward incompatibility, and this proposal would break any code that relies on that behaviour.

It’s not “undefined or not handled” at all:

>>> list(range(30, 3))
[]

range returns an iterator. Making it return an integer (-1) in some cases, but an iterator everywhere else, would be extremely difficult to work with. Python doesn’t use “special return values” (like -1) for error situations, it uses exceptions. But to reiterate, a start value higher than the end value isn’t an error situation - it simply defines an empty range.

7 Likes

No, just an iterable.

1 Like