Today in looking at other people's code to see such a way of writing, feeling is a very easy to step on the pit, make sure to write down the memo.
Short Circuit logic
When doing logical operations in Python, the default is to use an arithmetic rule called short-circuit logic. The name is very image, the following directly see the code
Print True and # 1print true or # Trueprint false and # Falseprint false or # 1
As you can see, although 1 is evaluated as a Boolean value, the entire expression is not necessarily a Boolean, but differs depending on the expression. The result of the above several expressions is the short-circuit logic. In plain English, once Python can tell if the entire expression is true or false, no subsequent calculations are made, that is, the logic is shorted, and subsequent expressions are ignored.
For example, the result is that, True or 1 True regardless of or any value, the result of the whole equation must be true, so Python sees, see, the True or back of the 1 is not seen, 1 is shorted, returned True ; False and 1 , and see False then and whatever is behind it, the result of the whole equation must be False , so 1 is shorted out and returned False .
Accordingly, why False or 1 would return 1, is because False and can or not judge the result of the formula, the whole equation is true or false, is determined by or the value of the following, so Python to read the entire expression, so return 1.
Understand what the short-circuit logic is, let's take a look at the three-mesh operator
Trinocular operator
The trinocular operator, also called the ternary operator. A classmate familiar with Java may know that its form is this b?x:y , for this expression, if the condition b is, the result of the True expression is x , if it is, the b False result of the expression is y . This is a very handy way of writing, is much more concise than the IF statement.
But in Python, there is no similar three-mesh operator in the early stages, so there is a way to use short-circuit logic to and or simulate the three-mesh operator, which we look at in the code
A = X and ' table ' or False
This code is in the use of short-circuit logic to simulate the three-mesh operator, when the X True time, will be assigned to, when it is, A 'table' will be X Flase A assigned value False ;
At first glance seems very good, but this writing has a hole, we look at this code
True and 0 or 1
If we use the same syntax as the three-mesh operator, then the conditional statement is True that the expression should return the 0 result we expect, but the expression is actually returned, 1 because the value of the True and 0 True entire expression is or the value of the After the value is determined, so Python will judge the value behind, resulting in the return 1 , which is why this writing and...or has a hole in the reason
Correct wording
If you want to use the three mesh operator in Python, you can use the if...else notation to see the code
# < results when True > if < criteria > Else < false results >0 if True else 1
if...elseThe result will be the same as we expected, and when the condition is True , return the previous value, when the condition is, False return the value that follows. There's no pit, just write it. Note that the order of conditions and return values is different from the three-mesh operator in Java, so don't make a mistake.