关于 Python3 中身份和布尔值的问题
A question about identity and boolean in Python3
我对 python 中的身份有疑问,我是 python 的初学者,我已经阅读了一些关于 "is" 关键字和 "is not" 的课程。而且我不明白为什么 "False is not True is not True is not False is not True" 中的 python 操作等于 False ?对我来说这个操作必须 return True.
你在处理逻辑问题。考虑 True = 1 和 False = 0 会有所帮助。
这样想。 0 is not 1, that will return True 因为数字 0 不是数字 1 并且是一个正确的语句。与 True 和 False
相同的概念
0 is not 1
#this will return False
False is not True
#the computer reads this in the exact same manner.
is
涉及 身份。
当你问if x is y
时,你真的是在问x
和y
是同一个对象吗? (请注意,这是一个不同于 do x
and y
have the same value?)
同样,当您问 if x is not y
时,您实际上是在问 是 x
和 y
不同的对象吗?
特别是关于 True
和 False
,Python 将它们视为 单例 ,这意味着只有一个 False
整个程序中的对象。任何时候你将某个东西分配给 False
,这是对单个 False
对象的 引用 ,因此所有 False
对象都具有相同的 身份.
Python chains comparisons:
Formally, if a, b, c, …, y, z
are expressions and op1, op2, …, opN
are comparison operators, then a op1 b op2 c ... y opN z
is equivalent to a op1 b and b op2 c and ... y opN z
, except that each expression is evaluated at most once.
你的表达是:
False is not True is not True is not False is not True
变成:
(False is not True) and (True is not True) and (True is not False) and (False is not True)
相当于:
(True) and (False) and (True) and (True)
也就是False
.
我对 python 中的身份有疑问,我是 python 的初学者,我已经阅读了一些关于 "is" 关键字和 "is not" 的课程。而且我不明白为什么 "False is not True is not True is not False is not True" 中的 python 操作等于 False ?对我来说这个操作必须 return True.
你在处理逻辑问题。考虑 True = 1 和 False = 0 会有所帮助。
这样想。 0 is not 1, that will return True 因为数字 0 不是数字 1 并且是一个正确的语句。与 True 和 False
相同的概念0 is not 1
#this will return False
False is not True
#the computer reads this in the exact same manner.
is
涉及 身份。
当你问if x is y
时,你真的是在问x
和y
是同一个对象吗? (请注意,这是一个不同于 do x
and y
have the same value?)
同样,当您问 if x is not y
时,您实际上是在问 是 x
和 y
不同的对象吗?
特别是关于 True
和 False
,Python 将它们视为 单例 ,这意味着只有一个 False
整个程序中的对象。任何时候你将某个东西分配给 False
,这是对单个 False
对象的 引用 ,因此所有 False
对象都具有相同的 身份.
Python chains comparisons:
Formally, if
a, b, c, …, y, z
are expressions andop1, op2, …, opN
are comparison operators, thena op1 b op2 c ... y opN z
is equivalent toa op1 b and b op2 c and ... y opN z
, except that each expression is evaluated at most once.
你的表达是:
False is not True is not True is not False is not True
变成:
(False is not True) and (True is not True) and (True is not False) and (False is not True)
相当于:
(True) and (False) and (True) and (True)
也就是False
.