Comparison operators can be chained in python, so that for example x < y < z
should give the result of (x < y) and (y < z)
, except that y
is guaranteed to be evaluated only once.
The abstract syntax tree of this operation looks like:
>>> ast.dump(ast.parse('0 < 1 < 2'), annotate_fields=0)
'Module([Expr(Compare(Num(0), [Lt(), Lt()], [Num(1), Num(2)]))])'
Pretty printed:
Module
Expr
Compare
Num
Lt
Lt
Num
Num
But it seems to parse as something like 0 < < 1 2
and I'm not sure how to reconcile that with the logical result of something like 0 < 1 and 1 < 2
.
How can the ast for chained comparisons be explained?