Python是否具有三元条件运算符? [英] Does Python have a ternary conditional operator?

查看:155
本文介绍了Python是否具有三元条件运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果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为true时,仅评估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.

请记住,一些Pythonista对此不以为然,

Keep in mind that it's frowned upon by some Pythonistas for several reasons:

  • 参数的顺序与许多其他语言(例如C,C ++,Go,Perl,Ruby,Java,Javascript等)中经典的condition ? a : b三元运算符的顺序不同,这可能会导致错误当人们不熟悉Python的令人惊讶"行为时,会使用它(他们可能会颠倒参数顺序).
  • 有人认为它笨拙",因为它违背了正常的思维流程(首先思考条件,然后思考效果).
  • 风格上的原因. (尽管'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大声读为x will be 4 if b is greater than 8 otherwise 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.

官方文档:

  • Conditional expressions
  • Is there an equivalent of C’s "?:" ternary operator?

这篇关于Python是否具有三元条件运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆