The rationale for this is really mentioned in ast docs
-- need sequences for compare to distinguish between -- x < 4 < 3 and (x < 4) < 3 | Compare(expr left, cmpop* ops, expr* comparators)
If it has been rated as two separate comparisons, for example
Module(Expr(Compare(Compare(Num(0), [Lt()], [Num(1)]), [Lt()], [Num(2)]))])
Then he actually compares the logical result of the first comparison with the integer in the second comparison.
Something like this will not work
-5 < -4 < -3
Since it will be rated as
(-5 < -4) < -3
What is rated as
1 < -3
Therefore, instead, it was considered as one expression. The python implementation of the Compare operation will look something like this:
def Compare(left, ops, comparators): if not ops[0](left, comparators[0]): return False for i, comparator in enumerate(comparators[1:], start=1): if not ops[i](comparators[i-1], comparator): return False return True
Brendan abel
source share