I’m having trouble understanding the following line:
result = (status!=None and "off" or "on")
Now when this is called, only status has been assigned a value (the result of a regular expression search). However, after this command, result takes the value of “on” or “off”.
To my untrained eye, this just looks like a comparison. How is this assignment happening?
From the documentation:
Furthermore, any non-empty string is considered to be "true" when it appears in a boolean context.
With this, we can tackle your question.
Due to operator precedence rules, the following:
is equivalent to:
The first part,
status != None and "off"evaluates to:FalseifstatusisNone;"off"otherwiseThe entire expression,
(status != None and "off") or "on"evaluates to:"on"ifstatusisNone;"off"otherwise.An arguably more Pythonic way to write this statement is: