X 和 Y 或 Z - 三元运算符
X and Y or Z - ternary operator
在 Java 或 C 中我们有 <condition> ? X : Y
,将 Python 转换为 X if <condition> else Y
。
不过还有这个小技巧:<condition> and X or Y
。
虽然我知道它等同于上述三元运算符,但我发现很难理解 and
和 or
运算符如何产生正确的结果。这背后的逻辑是什么?
While I understand that it's equivalent to the aforementioned ternary
operators
这是不正确的:
In [32]: True and 0 or 1
Out[32]: 1
In [33]: True and 2 or 1
Out[33]: 2
为什么第一个表达式returns1
(即Y
),而条件是True
而"expected"答案是0
(即 X
)?
根据文档:
The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is
returned.
The expression x or y first evaluates x; if x is true, its value is
returned; otherwise, y is evaluated and the resulting value is
returned.
因此,True and 0 or 1
计算 and
运算符的第一个参数,即 True
。然后是returns第二个参数,也就是0
.
由于True and 0
returns false值,or
运算符returns第二个参数(即1
)
我认为首先它会检查 <condition>
如果它是 True
然后它执行 X
并跳过执行 Y
如果 X
评估为 True
但是如果 <condition>
失败,那么它会跳过执行 X
并执行 OR 语句并执行 Y
。
如果我们检查 A and B
,只有当 A
是 True
时,B
才会被评估。
像这样,在A or B
中,B
只会在A
是False
的情况下被评估。
因此,如果 <condition>
是 True
,<condition> and X or Y
将 return X
,如果 <condition>
是 [=],Y
19=]。这是短路的结果,并且 and
优先于 or
。
但是,您应该谨慎使用这种方法。如果 X
本身被评估为 False
(例如空字符串、列表或 0
),<condition> and X or Y
将 return Y
即使 <condition>
是 True
:
X = 1
Y = 2
print(True and X or Y)
>> 1
相比于:
X = 0 # or '' or []
Y = 2
print(True and X or Y)
>> 2
诀窍在于 python 布尔运算符 work
The expression x and y
first evaluates x
; if x
is false, its value is returned; otherwise, y
is evaluated and the resulting value is returned.
The expression x or y
first evaluates x
; if x
is true, its value is returned; otherwise, y
is evaluated and the resulting value is returned.
这利用了 and
的优先级高于 or
的事实。
所以 <condition> and X or Y
基本上就是 (<condition> and X) or Y
。如果 <condition> and X
的计算结果为 True
,则无需进一步计算,因为任何 True or Y
都是 True
。如果 <condition> and X
的计算结果为 False
,则返回 Y,因为 False or Y
基本上是 Y
.
在 Java 或 C 中我们有 <condition> ? X : Y
,将 Python 转换为 X if <condition> else Y
。
不过还有这个小技巧:<condition> and X or Y
。
虽然我知道它等同于上述三元运算符,但我发现很难理解 and
和 or
运算符如何产生正确的结果。这背后的逻辑是什么?
While I understand that it's equivalent to the aforementioned ternary operators
这是不正确的:
In [32]: True and 0 or 1
Out[32]: 1
In [33]: True and 2 or 1
Out[33]: 2
为什么第一个表达式returns1
(即Y
),而条件是True
而"expected"答案是0
(即 X
)?
根据文档:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
因此,True and 0 or 1
计算 and
运算符的第一个参数,即 True
。然后是returns第二个参数,也就是0
.
由于True and 0
returns false值,or
运算符returns第二个参数(即1
)
我认为首先它会检查 <condition>
如果它是 True
然后它执行 X
并跳过执行 Y
如果 X
评估为 True
但是如果 <condition>
失败,那么它会跳过执行 X
并执行 OR 语句并执行 Y
。
如果我们检查 A and B
,只有当 A
是 True
时,B
才会被评估。
像这样,在A or B
中,B
只会在A
是False
的情况下被评估。
因此,如果 <condition>
是 True
,<condition> and X or Y
将 return X
,如果 <condition>
是 [=],Y
19=]。这是短路的结果,并且 and
优先于 or
。
但是,您应该谨慎使用这种方法。如果 X
本身被评估为 False
(例如空字符串、列表或 0
),<condition> and X or Y
将 return Y
即使 <condition>
是 True
:
X = 1
Y = 2
print(True and X or Y)
>> 1
相比于:
X = 0 # or '' or []
Y = 2
print(True and X or Y)
>> 2
诀窍在于 python 布尔运算符 work
The expression
x and y
first evaluatesx
; ifx
is false, its value is returned; otherwise,y
is evaluated and the resulting value is returned.The expression
x or y
first evaluatesx
; ifx
is true, its value is returned; otherwise,y
is evaluated and the resulting value is returned.
这利用了 and
的优先级高于 or
的事实。
所以 <condition> and X or Y
基本上就是 (<condition> and X) or Y
。如果 <condition> and X
的计算结果为 True
,则无需进一步计算,因为任何 True or Y
都是 True
。如果 <condition> and X
的计算结果为 False
,则返回 Y,因为 False or Y
基本上是 Y
.