问题描述
如果 Python 没有三元条件运算符,是否可以使用其他语言构造模拟一个?
If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
推荐答案
是的,它是 在 2.5 版中添加.表达式语法为:
Yes, it was added in version 2.5. The expression syntax is:
a if condition else b
首先评估 condition
,然后根据 condition
的值.如果 condition
计算结果为 True
,则计算并返回 a
但忽略 b
,否则当 >b
被评估并返回,但 a
被忽略.
First condition
is evaluated, then exactly one of either a
or b
is evaluated and returned based on the Boolean value of condition
. If condition
evaluates to True
, then a
is evaluated and returned but b
is ignored, or else when b
is evaluated and returned but a
is ignored.
这允许短路,因为当 condition
为真时,只计算 a
而根本不计算 b
,但是当 condition
为假,仅对 b
求值,而 a
根本不求值.
This allows short-circuiting because when condition
is true only a
is evaluated and b
is not evaluated at all, but when condition
is false only b
is evaluated and a
is not evaluated at all.
例如:
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'
请注意,条件是一个表达式,而不是一个语句.这意味着您不能在条件表达式中使用赋值语句或pass
或其他语句:
Note that conditionals are an expression, not a statement. This means you can't use assignment statements or pass
or other statements within a conditional expression:
>>> pass if False else x = 3
File "<stdin>", line 1
pass if False else x = 3
^
SyntaxError: invalid syntax
但是,您可以使用条件表达式来分配变量,如下所示:
You can, however, use conditional expressions to assign a variable like so:
x = a if True else b
将条件表达式视为两个值之间的切换.当您处于一种价值或另一种价值"的情况下时,它非常有用,但不会做太多其他事情.
Think of the conditional expression as switching between two values. It is very useful when you're in a 'one value or another' situation, it but doesn't do much else.
如果需要使用语句,则必须使用普通的if
语句,而不是条件表达式.
If you need to use statements, you have to use a normal if
statement instead of a conditional expression.
请记住,一些 Pythonistas 不赞成这样做的原因有几个:
Keep in mind that it's frowned upon by some Pythonistas for several reasons:
- 参数的顺序与经典的
条件不同?a :b
来自许多其他语言(如 C、C++、Go、Perl、Ruby、Java、Javascript 等)的三元运算符,当人们不熟悉 Python 的惊喜"时,可能会导致 bug.行为使用它(他们可能会颠倒参数顺序). - 有些人认为它笨拙",因为它违背了正常的思维流程(先考虑条件,然后考虑效果).
- 风格原因.(尽管内联
if
"可以真的有用,并使您的脚本更加简洁,但它确实会使您的代码复杂化)
- The order of the arguments is different from those of the classic
condition ? a : b
ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, Javascript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order). - Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
- Stylistic reasons. (Although the 'inline
if
' can be really useful, and make your script more concise, it really does complicate your code)
如果您在记住顺序时遇到困难,请记住,当您大声朗读时,您(几乎)说出了您的意思.例如,x = 4 如果 b >8 else 9
被朗读为 x 为 4 如果 b 大于 8 否则为 9
.
If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9
is read aloud as x will be 4 if b is greater than 8 otherwise 9
.
官方文档:
这篇关于Python 有三元条件运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!