Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

python - Python是否具有三元条件运算符?(Does Python have a ternary conditional operator?)

如果Python没有三元条件运算符,是否可以使用其他语言构造来模拟一个?

  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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布尔值对ab的任意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:

(官方文件:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...