Python one liner conditional -
can please explain code for?
shift, range = order < self.order , (1, (to, orig-1)) or (-1, (orig+1, to)) i know set shift , range depending on result of order < self.order
what don't know why there and , or statement there
from documentation of boolean expressions in python -
the expression
x , yfirst evaluatesx; ifxfalse, value returned; otherwise,yevaluated , resulting value returned.the expression
x or yfirst evaluates x; ifxtrue, value returned; otherwise, y evaluated , resulting value returned.
so and / or expressions in python don't return true or false, instead -
for
andreturn last evaluatedfalsevalue, or if valuestrue, return last evaluated value.for
orreturns last evaluatedtruevalue, or if valuesfalse, returns last evaluated value.
so , in case if condition true , returns value of (1, (to, orig-1)) , otherwise returns value of (-1, (orig+1, to)) .
very simple examples show -
>>> 1 < 5 , 2 or 4 2 >>> 6 < 5 , 2 or 4 4 also, though not directly applicable condition , in general case, condition -
cond , or b if cond true-like value , a has false-like value (like 0 or empty string/list/tuple, etc), return b . example -
>>> 1 < 5 , 0 or 2 2 so, better use equivalent if..else ternary expression, example -
a if cond else b
Comments
Post a Comment