The Python language has a neat feature: An expression like x < y <= z
is interpreted, according to mathematical convention, as equivalent to x < y and y <= z
. Operands are evaluated only as many times as they appear, and the short-circuiting nature of the equivalent operation is maintained. For example,
if f() <= g() < h() <= i(): whatever
behaves as if the code were
x1 = f()
x2 = g()
if x1 <= x2:
x3 = h()
if x2 < x3:
x4 = i()
if x3 <= x4:
whatever
(Thus h()
and i()
aren't evaluated unless necessary.)
As far as I am aware, the first language to support this was CPL, as demonstrated on page 6.1.3 of the CPL working papers. However, CPL was never implemented fully, so it could be argued that the feature was never supported, per se. Were there earlier examples?
a < b < c
means something entirely different from(a < b) < c
ora < (b < c)
will be more complicated—harder to write, harder to understand—than the parsers used in other programming languages. And, if it's harder for a program to parse the expression, then it's harder for a human to understand it too. Of course, once you've learned what it means, then it's easy enough to see, but before you could "see" it you had to learn one extra thing about Python expressions than you didn't learn for other languages.