Python 如何处理同时具有赋值和比较的语句?
How does Python process a statement with both assignment and a comparison?
我有以下行:
group_index = apps["special_groups"] == group
根据我的理解,group_index
被赋予了 apps["special_groups"]
中的值。然后我看到 == 运算符,但它对结果有什么作用?还是比较apps["special_groups"]
先分组?
它将布尔比较[True
或False
]的值赋给LHS变量group_index
。
来自 Python Evaluation Order 文档:
Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
所以它首先评估apps["special_groups"] == group
,然后将其结果分配给group_index
。
来自 Python 文档,Section 5.14:
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
因此它首先计算右侧 apps["special_groups"] == group
,然后将此结果分配给左侧 group_index
。
我有以下行:
group_index = apps["special_groups"] == group
根据我的理解,group_index
被赋予了 apps["special_groups"]
中的值。然后我看到 == 运算符,但它对结果有什么作用?还是比较apps["special_groups"]
先分组?
它将布尔比较[True
或False
]的值赋给LHS变量group_index
。
来自 Python Evaluation Order 文档:
Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
所以它首先评估apps["special_groups"] == group
,然后将其结果分配给group_index
。
来自 Python 文档,Section 5.14:
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
因此它首先计算右侧 apps["special_groups"] == group
,然后将此结果分配给左侧 group_index
。