Add 'inrange()' simple function

The analog of ‘i in range(5, 15)’ can be created, something like inrange() named, which expects 2-3 arguments and returns True or False.

The first argument is the number.
If only 2 arguments are set, then the range will be from 0 to the second argument.
If 3 arguments are set, then the range will be from second to the third argument.

Examples for use:
if inrange(7, 5, 15):
print(“Yes“)

if inrange(7, 15):
print(“Yes“)

Suggestion update:

  1. The example inrange(7.0, 5, 15) is more intuitive.
  2. The name between(7.0, 5, 15) seems more appropriate than inrange(7.0, 5, 15).

But what do you find wrong with i in range(5, 15)?

10 Likes

Nothing is wrong with i in range(5, 15) - It’s perfectly valid Python.
The motivation for inrange() is semantic clarity: it acts as a boolean predicate that clearly communicates a bounds check.

For example, inrange(x, min, max) can be more expressive in validation logic, configuration checks, or APIs, where the concept is “range checking” rather than iteration.

To me a in range(b, c) communicates the intent of a bounds check on a much more clearly than inrange(a, b, c), which would leave me guessing if it means a in range(b, c), c in range(a, b) or even b in range(a, c) upon reading the code for the first time.

You can trivially write a wrapper function with your desired specs if that is what you find more readable, but I just don’t think it’s more expressive to most than the status quo.

19 Likes

Or a <= b < c (or any other permutation), which isn’t the same thing.

15 Likes

The max name makes me wonder whether you even mean the equivalent of in range, as the maximum of a set is included. What would inrange(6, 3, 6) return? And why don’t you just use min <= x < max? (Or <= max?)

Also, there’s no iteration in 7 in range(5, 15).

3 Likes

In my opinion, the syntax ‘not inrange(x, min, max)’ is more readable for begginers than ‘not min < x < max’.

That’s a matter of opinion, but fortunately, you can define that function yourself for your own benefit :slight_smile: You can have things the exact way you want them, without needing anyone else’s approval!

3 Likes

So not even min is included? Seems really inconsistent / bad.

1 Like

Is it supposed to be semantically identical to x in range(a, b)?

What about inrange(1.0, 0, 3)?

Isn’t inrange(low, element, high) even more intuitive?

2 Likes

Yes, that might be a good example for it, and it’s more intuitive.

Thanks for the feedback.

Also, the name between(7.0, 5, 15) seems more appropriate to me than inrange(7.0, 5, 15).

1 Like

Thanks for sharing your idea. However, to set expectations and possible shorten a lengthy discussion, I project this has zero chance of acceptance.

First, between(a, b, c) has clarity issues itself: It’s not canonically clear which of the arguments is the value and which are the bounds. It’s also not clear whether the borders are included. Even worse, there are legitimate use cases for all combinations of (not) including borders. This would mean you need four functions or additional parameters like between(a, b, c, include_left=True, include_right=False) which will destroy any potential readability advantage.

Second, the readability advantage is very subjective. I can see that many people will regard a < val < b as more readable compared to between(val, a, b). Also the negated version doesn’t change that: not (a < val < b) vs. not between(val, a, b).

Python strongly prefers “one way to do it” and there are very high bars for introducing new stdlib functions or even builtins. I can see no way how any interpretation/variation of the two aforementioned points can overcome that bar.

14 Likes

It sounds to me like you want Integer Interval Arithmetic, which is probably best done as a Pypi module.

1 Like