[英]What does 'is' operator do in Python?
I was (maybe wrongfully) thinking that is
operator is doing id() comparison. 我(也许非法),其思想
is
运营商正在做ID()进行比较。
>>> x = 10
>>> y = 10
>>> id(x)
1815480092
>>> id(y)
1815480092
>>> x is y
True
However, with val is not None
, it seems like that it's not that simple. 然而,由于
val is not None
,看起来并不是那么简单。
>>> id(not None)
2001680
>>> id(None)
2053536
>>> val = 10
>>> id(val)
1815480092
>>> val is not None
True
Then, what does 'is' operator do? 然后,'是'运营商做了什么? Is it just object id comparison just I conjectured?
它只是我推测的对象id比较吗? If so,
val is not None
is interpreted in Python as not (val is None)
? 如果是这样,
val is not None
在Python中解释not (val is None)
?
You missed that is not
is an operator too . 你错过了
is not
是一个操作符太 。
Without is
, the regular not
operator returns a boolean: 没有
is
,经常not
运算符返回一个布尔值:
>>> not None
True
not None
is thus the inverse boolean 'value' of None
. not None
因此的逆布尔“值” None
。 In a boolean context None
is false: 在布尔上下文中,
None
为false:
>>> bool(None)
False
so not None
is boolean True
. 所以
not None
是布尔True
。
Both None
and True
are objects too, and both have a memory address (the value id()
returns for the CPython implementation of Python): None
和True
都是对象,并且都有一个内存地址(值为id()
返回Python的CPython实现):
>>> id(True)
4440103488
>>> id(not None)
4440103488
>>> id(None)
4440184448
is
tests if two references are pointing to the same object ; is
测试两个引用是否指向同一个对象 ; if something is the same object, it'll have the same id()
as well. 如果某个东西是同一个对象,它也会有相同的
id()
。 is
returns a boolean value, True
or False
. is
返回一个布尔值, True
或False
。
is not
is the inverse of the is
operator. is not
is
运算符的反转。 It is the equivalent of not (op1 is op2)
, in one operator. 它在一个运算符中等效于
not (op1 is op2)
。 It should not be read as op1 is (not op2)
here: 它不应该被理解为
op1 is (not op2)
在这里:
>>> 1 is not None # is 1 a different object from None?
True
>>> 1 is (not None) # is 1 the same object as True?
False
As a complement to Martijn Pieters answer, None
, True
and False
are singletons. 作为Martijn Pieters答案的补充,
None
, True
和False
是单身人士。 There is only one instance of each of these values so it is safe to use is
to test them.. 只有一个每个值的实例,以便它是安全的使用
is
对它们进行测试..
>>> id(True)
8851952
>>> id(False)
8851920
>>> id(None)
8559264
>>> id(not None)
8851952
>>> id(1 == 0)
8851920
>>> id(1 == 1)
8851952
Python tries to emulate the English language syntax to make it more human readable, but in this instance the operator precedence is a little confusing. Python试图模仿英语语法,使其更具人性化,但在这种情况下,运算符优先级有点令人困惑。
>>> val is not None
True
In English, we are asking if val
does not have a value that is represented by the same object as None
. 在英语中,我们询问
val
是否没有由与None
相同的对象表示的值。 In your above example val=10
, so the answer is (correctly) True
. 在上面的例子中
val=10
,所以答案是(正确)为True
。
However, from a logical, syntactical viewpoint you have broken down the statement word-for-word and assumed it to be: 但是,从逻辑的,语法的角度来看,你已经逐字逐句地分解了这个陈述并假设它是:
>>> val is (not None)
False
which, when enclosed in appropriate patentheses, returns your expected result ( False
). 如果附在适当的专利中,则返回您的预期结果(
False
)。
As @Martijn Pieters points out, is not
is an operator in its own right , and it the only operator at work here. 正如@Martijn Pieters指出的那样, 它本身
is not
一个运营商 ,它是唯一在这里工作的运营商。
If you wanted to write it as a non ambiguous statement using only operators that contain no spaces, you could write: 如果您只想使用不包含空格的运算符将其写为非模糊语句,则可以编写:
>>> not (val is None)
True
But that is not how you would 'say' the intended statement in English, or possibly even write it in pseudo-code. 但这并不是你如何用英语“说”预期的语句,甚至可能用伪代码写出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.