Background
As we all known, we'd better not assginment several variables use chain assignment like a = b = [1,2,3], because a will be a shalow copy of b. It is not safe because a will change when we revise b.
However, if the initialization is immutable, we can do like this a = b = 1 and it's safe.
Recently, I find a strange usage of multiple operators in the condition expression of a control flow, like if 1 < b < 2: or while a == b == c == 1:
For example, the following control flow excute different chunks under different conditions:
a = 1
b = 1
c = 2
if a == b == c == 1:
print('All equal!')
else:
print('At least one variable is not equal to others')
At least one variable is not equal to others
My question
Is this multiple operation usage safe in a boolean expression in within a control flow? I know we should check the operator precedence when we write a boolean expression. Is there anything else we should keep an eye on? I try for a while and I think the multiple operator usage is safe.
Bytecode analysis
I type the bytecode of the following program:
a = 1;b =2;c =1.5
a<b<c
import dis
dis.dis('a<b<c')
1 0 LOAD_NAME 0 (a) 2 LOAD_NAME 1 (b) 4 DUP_TOP 6 ROT_THREE 8 COMPARE_OP 0 (<) 10 JUMP_IF_FALSE_OR_POP 18 12 LOAD_NAME 2 (c) 14 COMPARE_OP 0 (<) 16 RETURN_VALUE > 18 ROT_TWO 20 POP_TOP 22 RETURN_VALUE```
I can only recognize that it compare a and b at step 10 and then compare a and c at step 14. But why it still return False. I not familiar with analysing bytecode. If someone can help with analysing it, I will be very appreciated! Here is an official guide of Module: dis
a = b = [1, 2, 3]assignment.assgnment. I have no question about that. All I wanna know is whether multiple operation usage is safe in aboolean expressionin within a control flow?multiple boolean expression.