Hacker News new | ask | show | jobs
by thaumasiotes 2755 days ago
That... has nothing to do with nemo1618's wish. The python statement 2 <= x < 4 is not a statement about x being within a particular range; it's just as valid to say 2 <= x > 4.
1 comments

Nobody would ever purposefully write 2 <= x > 4 … it's a bug. Maybe if the bounds on the example were variable.

But the point of being able to collapse a < b and b < c into a < b < c is there to make ranges easier to notate/read. That's seems to be nemo's wish. (Though perhaps he wishes the range to be completely separate, e.g., in the psuedosyntax `b in [1, 3)`.)

I don't think I've ever seen it actually used for anything else, and I would in advocate against it, in code review.

> I don't think I've ever seen it actually used for anything else, and I would in advocate against it, in code review.

Yes, this would be a terrible practice. But that's what Python offers. The desire to express that a variable's value lies within a range is very common, and is presumably the reason for Python's bad choice. But that doesn't make Python's syntax a good response to the desire. The syntax expresses a conceptual mess; you're relying on people only using a tiny subset of what's there.

Contrast lisp, where you actually can express the concept of a range, as (< lowBound variable highBound). Unfortunately, that won't allow you to mix soft bounds with strict bounds. But it's vastly better than Python's approach.

Contrast Ruby, which offers almost every convenience in ranges, including literal notations, that you could ask for.