不(不假)=真?

not (not false) = True?

我目前在代码学院 Python 的条件和控制流页面上。

我认为这是错误的,但这是错误的。

骗我!

bool_three = not (not False) == True

括号里的东西先算出来,所以按我的逻辑:

not (not False [which becomes True]) = True

not True [which is false] = True

bool_three = 不(不假)== 真

就是这样:

不(不假)变成不(真)变成.

然后 False == True(这是错误的)

那么 bool_three = false

not (not False [which becomes True]) = True

是什么让您认为 "not not false" 是正确的?如果一个布尔值被否定,它变成相反的值。如果再次取反,则变成原来的值

让我们一步一个脚印...

  1. 不(不假)== 真
  2. 不(正确)==正确
  3. 假 == 真

快速 Python 解释器检查:

>>> not not False == True
False