简体   繁体   English

为什么 Python 从 1 或 0 切换到 True 或 False?

[英]Why does Python switch from 1 or 0 to True or False?

I'm learning about logic gates and was messing around with them when I found something interesting.我正在学习逻辑门,当我发现一些有趣的东西时,我正在弄乱它们。 This is my code:这是我的代码:

for i in range (4):
    p = math.floor(i/2)
    q = i % 2
    a = p and q 
    b = not(not(p or q) or not(q) or not(p))
        
    print(str(p) +"\t"+ str(q) + "\t"+ str(a)+"\t"+str(b))

And this is the result:这是结果:

0       0       0       False
0       1       0       False
1       0       0       False
1       1       1       True

Why doesn't Python either always print a 1 or a 0 or always print True or False ?为什么 Python 不总是打印10或者总是打印TrueFalse

math.floor returns a number. math.floor返回一个数字。

Operators between two numbers (such as modulo) also return numbers.两个数字之间的运算符(例如取模)也返回数字。

not returns a bool , not an int not返回bool ,而不是int

Because not has to create a new value, it returns a boolean value regardless of the type of its argument - spec因为not创建新值,所以无论其参数类型如何,它都会返回一个 boolean 值 -规范

If you want it to be an int (and simplify the print statement)如果您希望它是一个 int (并简化 print 语句)

print('\t'.join(map(str, [p, q, a, int(b)])

The not unary operator always returns a bool value. not一元运算符总是返回一个bool值。 It needs to, as it needs to return the boolean inverse of whatever value it was passed, which is always a new value (not its argument).它需要,因为它需要返回传递给它的任何值的 boolean 的倒数,它始终是一个新值(不是它的参数)。

The real odd part of your chart is actually the third column, which shows that the and operator does return numbers if it's passed them on both sides.图表中真正奇怪的部分实际上是第三列,它表明and运算符确实返回数字,如果它在两边都传递的话。 You might expect and to be a boolean operator just like not , but it's slightly different.您可能期望and成为 boolean 运算符,就像not一样,但它略有不同。 The reason is that and always returns one of its arguments. If the first argument is falsey, that value is what is returned.原因是and始终返回其 arguments 中的一个。如果第一个参数为假,则返回该值。 If the first argument is truthy, the second argument is returned.如果第一个参数为真,则返回第二个参数。

You can see this if you test with values other than just 0 and 1 :如果您使用01以外的值进行测试,您会看到这一点:

print(3 and 2)       # prints 2
print([] and [1,2])  # prints []

The or operator also does this (in a slightly different manner), but that's covered up in your calculation of b by the not calls. or运算符也执行此操作(方式略有不同),但not调用在您的b计算中掩盖了这一点。

This behavior of and and or is called short-circuiting. andor的这种行为称为短路。 It's big advantage is that it lets Python avoid interpreting expressions it doesn't need to get the value of the boolean expression.它的一大优势是它让 Python 避免解释不需要获取 boolean 表达式值的表达式。 For instance, this expression will not call the slow_function :例如,这个表达式不会调用slow_function

result = falsey_value and slow_function()

It also lets you guard expressions that would cause exceptions if they were evaluated:它还可以让您保护在评估时会导致异常的表达式:

if node is not None and node.value > x:
     ...

If the node variable is None in that code, evaluating node.value would give an AttributeError (since None doesn't have a value attribute).如果该代码中的node变量为None ,则评估node.value将给出AttributeError (因为None没有value属性)。 But because and short circuits, the first expression prevents the second expression from being evaluated when it's invalid.但是因为and短路,第一个表达式阻止了第二个表达式在无效时被计算。

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

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