简体   繁体   English

使用和/或组合在python中实现三元运算符

[英]implementing ternary operator in python using and/or combinations

I am learning python using the excellent book by Mark Lutz. 我正在使用Mark Lutz的优秀著作来学习python。 I come across this statement that the ternary operator in python, which is effectively this: 我遇到了以下声明,即python中的三元运算符,实际上是这样的:

if a: 
   b
else: 
   c

can be written in 2 ways: 可以用两种方式编写:

  1. b if a else c : using normal ternary syntax of python and b if a else c :使用python的普通三元语法,并且

  2. ((a and b) or c) : using the equivalent but trickier and/or combination ((a and b) or c) :使用等效但比较棘手的and/or组合

I find the second representation disconcerting as it doesn't go well with my instinct. 我发现第二种表示方式令人不安,因为它与我的直觉并不吻合。 I tried these 2 syntax on the interactive prompt and found different answers for special case of b = 0. (assume b = 0, a = 4, c = 20) 我在交互式提示上尝试了这2种语法,并针对b = 0.特殊情况找到了不同的答案。(假设b = 0,a = 4,c = 20)

  1. 0 if 4 else 20 outputs 0 0 if 4 else 20输出0
  2. ((4 and 0) or 20) outputs 20 ((4 and 0) or 20)输出20

It appears that the 2 expressions are equivalents for all the truthy values of b but are not equivalent for all the falsy values of b . 看来,2个表达式是等效的所有truthy的值b ,但不等同于所有falsy的值b

I want to know, is there anything that I am missing here. 我想知道,这里有什么我想念的。 Is my analysis wrong? 我的分析错了吗? Why does it say so in the book that the two cases are equivalent. 为什么在书中这么说两个案例是相等的。 Please enlighten my coarse mind. 请启发我的粗心。 I am new to python. 我是python的新手。 Thanks in advance. 提前致谢。

You are right, the second approach is great in most cases. 没错,在大多数情况下,第二种方法很棒。

From the python docs: 从python文档中:

Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators: [expression] and [on_true] or [on_false] 在Python 2.5中引入此语法之前,一个常见的习惯用法是使用逻辑运算符:[expression]和[on_true]或[on_false]

Right after that they mention: 之后,他们提到:

"However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the ... if ... else ... form. “但是,这种习惯用法是不安全的,因为当on_true具有错误的布尔值时,它会产生错误的结果。因此,最好使用... if ... else ...形式。

Here's a reference: https://docs.python.org/3.3/faq/programming.html#is-there-an-equivalent-of-cs-ternary-operator 这是参考: https : //docs.python.org/3.3/faq/programming.html#is-there-an-equivalent-of-cs-ternary-operator

Adding brief sample per request: 为每个请求添加简要示例:

a = True
b = False
c = True

# prints False (for b) correctly since a is True
if a:
   print b
else: 
   print c

# prints False (for b) correctly since a is True
print b if a else c 

# prints True (for c) incorrectly since a is True and b should have been printed
print ((a and b) or c) 

The perspective of the author here is different which should be considered. 作者的观点在这里是不同的,应该加以考虑。 Let me try to explain with the code & inline comments: 让我尝试用代码和内联注释来解释:

#This if condition will get executed always(because its TRUE always for any number) except when it is '0' which is equivalent to boolean FALSE.
#'a' is the input which the author intends to show here. 'b' is the expected output
if a: 
   print(b)
else: 
   print(c)

#equivalent
print(b) if a else print(c) 
print((a and b) or c)

You are supposed to change the input and check the output. 您应该更改输入并检查输出。 Whereas, you change the OUTPUT directly and try to check the output, which is not working. 而您直接更改OUTPUT并尝试检查输出,则该输出不起作用。 So, you are testing the wrong way is my opinion. 因此,我认为您正在测试错误的方式。 The input here is a. 输入是一个。 Output here is b. 这里的输出是b。 Case 1: b = 12 a = 1 c = 20 情况1:b = 12 a = 1 c = 20

*Case 2:
b = 12
a = 0
c = 20*
*Dont change 'b'. Change only 'a' and test is the conceptual idea. Coz, 'b' is the output.*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM