使用 and/or 组合在 python 中实现三元运算符
implementing ternary operator in python using and/or combinations
我正在学习 python 使用 Mark Lutz 的优秀书籍。我遇到了这个声明,即 python 中的三元运算符,实际上是这样的:
if a:
b
else:
c
有两种写法:
b if a else c
:使用 python 和
的普通三元语法
((a and b) or c)
:使用等效但更棘手的 and/or
组合
我发现第二种表示方式令人不安,因为它不符合我的直觉。我在交互式提示中尝试了这两种语法,并针对 b = 0.
的特殊情况找到了不同的答案(假设 b = 0,a = 4,c = 20)
0 if 4 else 20
输出 0
((4 and 0) or 20)
输出 20
看来这 2 个表达式对于 b
的所有 truthy
值都是等价的,但对于 b
的所有 falsy
值并不等价。
我想知道,这里有什么我遗漏的吗?我的分析错了吗?为什么书上这么说,这两种情况是等价的。请开导我的粗心。我是 python 的新手。提前致谢。
你说得对,第二种方法在 大多数 情况下都很好。
来自 python 文档:
Before this syntax was introduced in Python 2.5, a common idiom was to
use logical operators: [expression] and [on_true] or [on_false]
紧接着他们提到:
"However, this idiom is unsafe, as it can give wrong results when
on_true has a false boolean value. Therefore, it is always better to
use the ... if ... else ... form.
参考资料如下:
https://docs.python.org/3.3/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator
为每个请求添加简短示例:
a = True
b = False
c = True
# prints False (for b) correctly since a is True
if a:
print b
else:
print c
# prints False (for b) correctly since a is True
print b if a else c
# prints True (for c) incorrectly since a is True and b should have been printed
print ((a and b) or c)
这里作者的视角不同,应该考虑一下。让我尝试用代码和内联注释来解释:
#This if condition will get executed always(because its TRUE always for any number) except when it is '0' which is equivalent to boolean FALSE.
#'a' is the input which the author intends to show here. 'b' is the expected output
if a:
print(b)
else:
print(c)
#equivalent
print(b) if a else print(c)
print((a and b) or c)
您应该更改输入并检查输出。然而,您直接更改 OUTPUT 并尝试检查输出,但这是行不通的。因此,我认为您正在以错误的方式进行测试。
这里的输入是a。
这里的输出是b。
案例一:
b = 12
一 = 1
c = 20
*Case 2:
b = 12
a = 0
c = 20*
*Dont change 'b'. Change only 'a' and test is the conceptual idea. Coz, 'b' is the output.*
我正在学习 python 使用 Mark Lutz 的优秀书籍。我遇到了这个声明,即 python 中的三元运算符,实际上是这样的:
if a:
b
else:
c
有两种写法:
b if a else c
:使用 python 和 的普通三元语法
((a and b) or c)
:使用等效但更棘手的and/or
组合
我发现第二种表示方式令人不安,因为它不符合我的直觉。我在交互式提示中尝试了这两种语法,并针对 b = 0.
的特殊情况找到了不同的答案(假设 b = 0,a = 4,c = 20)
0 if 4 else 20
输出0
((4 and 0) or 20)
输出20
看来这 2 个表达式对于 b
的所有 truthy
值都是等价的,但对于 b
的所有 falsy
值并不等价。
我想知道,这里有什么我遗漏的吗?我的分析错了吗?为什么书上这么说,这两种情况是等价的。请开导我的粗心。我是 python 的新手。提前致谢。
你说得对,第二种方法在 大多数 情况下都很好。
来自 python 文档:
Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators: [expression] and [on_true] or [on_false]
紧接着他们提到:
"However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the ... if ... else ... form.
参考资料如下: https://docs.python.org/3.3/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator
为每个请求添加简短示例:
a = True
b = False
c = True
# prints False (for b) correctly since a is True
if a:
print b
else:
print c
# prints False (for b) correctly since a is True
print b if a else c
# prints True (for c) incorrectly since a is True and b should have been printed
print ((a and b) or c)
这里作者的视角不同,应该考虑一下。让我尝试用代码和内联注释来解释:
#This if condition will get executed always(because its TRUE always for any number) except when it is '0' which is equivalent to boolean FALSE.
#'a' is the input which the author intends to show here. 'b' is the expected output
if a:
print(b)
else:
print(c)
#equivalent
print(b) if a else print(c)
print((a and b) or c)
您应该更改输入并检查输出。然而,您直接更改 OUTPUT 并尝试检查输出,但这是行不通的。因此,我认为您正在以错误的方式进行测试。 这里的输入是a。 这里的输出是b。 案例一: b = 12 一 = 1 c = 20
*Case 2:
b = 12
a = 0
c = 20*
*Dont change 'b'. Change only 'a' and test is the conceptual idea. Coz, 'b' is the output.*