问题描述
如果 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
,然后根据 a 或 b
中的一个://en.wikipedia.org/wiki/Boolean_data_type" rel="noreferrer" title="布尔数据类型">布尔 条件
的值.如果 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
为 false,仅评估 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.
请记住,一些 Python 爱好者不赞成它有几个原因:
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.行为使用它(他们可能会颠倒参数顺序). - 有些人认为它笨拙",因为它与正常的思维流程背道而驰(先考虑条件,然后再考虑效果).
- 风格原因.(虽然 'inline
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 if b >;8 else 9
被大声读出,因为 如果 b 大于 8,x 将为 4,否则为 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
.
官方文档:
- 条件表达式李>
- 是否有等价于 C 的?:"三元运算符?
这篇关于Python有三元条件运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!