Yes, it was added in version 2.5.
(是的,它是在2.5版中添加的 。)
The expression syntax is: (表达式语法为:)
a if condition else b
First condition
is evaluated, then exactly one of either a
or b
is evaluated and returned based on the Boolean value of condition
.
(首先对condition
求值,然后根据condition
的布尔值对a
或b
的任意a
求值并返回。)
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
评估为True
,那么a
评估和返回,但b
被忽略,否则当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.
(这使得短路,因为当condition
是真实的,只有a
评价和b
是不是在所有的评估,但是,当condition
是假的只有b
进行评估, a
是不是在所有评估。)
For example:
(例如:)
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'
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
或其他语句 :)
>>> 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 you need to use statements, you have to use a normal if
statement instead of a conditional expression .
(如果需要使用语句,则必须使用常规的if
语句而不是条件表达式 。)
Keep in mind that it's frowned upon by some Pythonistas for several reasons:
(请记住,由于某些原因,某些Pythonista对此并不满意:)
- The order of the arguments is different from those of the classic
condition ? a : b
(论据的顺序与经典condition ? a : b
的顺序不同condition ? a : b
)
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). (condition ? a : b
来自许多其他语言(例如C,C ++,Go,Perl,Ruby,Java,Javascript等)的三元运算符,当人们不熟悉Python的“令人惊讶”行为时可能会导致错误(它们可能会逆转)参数顺序)。)
- 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) ((尽管'inline if
'可能确实有用,并且可以使脚本更简洁,但确实会使代码复杂化))
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
. (例如, x = 4 if b > 8 else 9
,则x = 4 if b > 8 else 9
大声读取x = 4 if b > 8 else 9
因为x will be 4 if b is greater than 8 otherwise 9
。)
Official documentation:
(官方文件:)