我一直在玩,發現了這個。運算符is和is not測試對象的身份:x is y 為真當且僅當 x 和 y 是同一對象時。對象的身份是使用該id()函數確定的。x 不是 y 會產生相反的真值。>>> list(map(id, [0, 1, True, not False, False, not True]))[94660352164256, 94660352164288, 94660351988128, 94660351988128, 94660351988096, 94660351988096]現在一切正常:0、1、True且False具有不同的 id,因為它們是不同的對象。他們是:>>> True is 1False>>> False is 0False>>> not False is TrueTrue>>> not True is FalseTrue但是之后:>>> not False is 1True>>> not True is 0True>>> 我的問題是: 、和全部返回怎么可能?True is not 1not False is Truenot False is 1True
2 回答

DIEA
TA貢獻1820條經驗 獲得超2個贊
那是因為is
之前not
。
https://docs.python.org/3/reference/expressions.html#operator-precedence
所以:
not False is 1 => not (False is 1) => not False => True
和:
not True is 0 => not (True is 0) => not False => True

慕少森
TA貢獻2019條經驗 獲得超9個贊
它與優先級和運算符優先級有關??梢赃@樣想:
(True is not 1)
, not (False is True)
, not (False is 1)
.
現在一切都應該有意義了。
添加回答
舉報
0/150
提交
取消