Python 为什么不能使用;是";比较一下数字?

Python 为什么不能使用;是";比较一下数字?,python,numbers,Python,Numbers,可能重复: 通常我使用type(x)==type(y)来比较类型是否相同。然后使用x==y比较数值是否相等 然而,有人提出,如果z1和z2包含具有完全相同值的相同类型的数字,则可以使用z1 is z2进行比较。在许多情况下,这将是成功的(特别是对于正整数) 但是,有时同一个数字(主要是负整数)可能有几个不同的实例。这是python的预期行为吗 例如: >>> for x in range(-20,125): z1=x z2=int(float(x))

可能重复:

通常我使用
type(x)==type(y)
来比较类型是否相同。然后使用
x==y
比较数值是否相等

然而,有人提出,如果
z1
z2
包含具有完全相同值的相同类型的数字,则可以使用
z1 is z2
进行比较。在许多情况下,这将是成功的(特别是对于正整数)

但是,有时同一个数字(主要是负整数)可能有几个不同的实例。这是python的预期行为吗

例如:

>>> for x in range(-20,125):
    z1=x
    z2=int(float(x))
    if z1 is not z2:
        print "z1({z1}; type = {typez1}; id={idz1}) is not z2({z2}; type = {typez2}; id={idz2})".format(z1=z1,typez1=type(z1),idz1=id(z1),z2=z2,typez2=type(z2),idz2=id(z2))


z1(-20; type = <type 'int'>; id=33869592) is not z2(-20; type = <type 'int'>; id=33870384)
z1(-19; type = <type 'int'>; id=33870480) is not z2(-19; type = <type 'int'>; id=33870408)
z1(-18; type = <type 'int'>; id=32981032) is not z2(-18; type = <type 'int'>; id=33870384)
z1(-17; type = <type 'int'>; id=33871368) is not z2(-17; type = <type 'int'>; id=33870408)
z1(-16; type = <type 'int'>; id=33869712) is not z2(-16; type = <type 'int'>; id=33870384)
z1(-15; type = <type 'int'>; id=33869736) is not z2(-15; type = <type 'int'>; id=33870408)
z1(-14; type = <type 'int'>; id=33869856) is not z2(-14; type = <type 'int'>; id=33870384)
z1(-13; type = <type 'int'>; id=33869280) is not z2(-13; type = <type 'int'>; id=33870408)
z1(-12; type = <type 'int'>; id=33868464) is not z2(-12; type = <type 'int'>; id=33870384)
z1(-11; type = <type 'int'>; id=33868488) is not z2(-11; type = <type 'int'>; id=33870408)
z1(-10; type = <type 'int'>; id=33869616) is not z2(-10; type = <type 'int'>; id=33870384)
z1(-9; type = <type 'int'>; id=33871344) is not z2(-9; type = <type 'int'>; id=33870408)
z1(-8; type = <type 'int'>; id=33869064) is not z2(-8; type = <type 'int'>; id=33870384)
z1(-7; type = <type 'int'>; id=33870336) is not z2(-7; type = <type 'int'>; id=33870408)
z1(-6; type = <type 'int'>; id=33870360) is not z2(-6; type = <type 'int'>; id=33870384)
>>> x
124
>>> print x
124
>>> import sys
>>> print sys.version
2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1]
范围(-20125)内x的
>:
z1=x
z2=int(浮点数(x))
如果z1不是z2:
打印“z1({z1};type={typez1};id={idz1})不是z2({z2};type={typez2};id={idz2})”。格式(z1=z1,typez1=type(z1),idz1=id(z1),z2=z2,typez2=type(z2),idz2=id(z2))
z1(-20;类型=;id=33869592)不是z2(-20;类型=;id=33870384)
z1(-19;类型=;id=33870480)不是z2(-19;类型=;id=33870408)
z1(-18;类型=;id=32981032)不是z2(-18;类型=;id=33870384)
z1(-17;类型=;id=33871368)不是z2(-17;类型=;id=33870408)
z1(-16;类型=;id=33869712)不是z2(-16;类型=;id=33870384)
z1(-15;类型=;id=33869736)不是z2(-15;类型=;id=33870408)
z1(-14;类型=;id=33869856)不是z2(-14;类型=;id=33870384)
z1(-13;类型=;id=33869280)不是z2(-13;类型=;id=33870408)
z1(-12;类型=;id=33868464)不是z2(-12;类型=;id=33870384)
z1(-11;类型=;id=33868488)不是z2(-11;类型=;id=33870408)
z1(-10;类型=;id=33869616)不是z2(-10;类型=;id=33870384)
z1(-9;类型=;id=33871344)不是z2(-9;类型=;id=33870408)
z1(-8;类型=;id=33869064)不是z2(-8;类型=;id=33870384)
z1(-7;类型=;id=33870336)不是z2(-7;类型=;id=33870408)
z1(-6;类型=;id=33870360)不是z2(-6;类型=;id=33870384)
>>>x
124
>>>打印x
124
>>>导入系统
>>>打印系统版本
2.7.2+(默认,2011年10月4日,20:06:09)
[GCC 4.6.1]

是。只有少数接近0的数字(正如您所发现的,正数大于负数)被编译器截取。由于表达式可能会导致一个超出此范围的数字,
is
不应用于检查相等性。

is的唯一正确用法是比较对象标识的相等性。要比较值的相等性,请使用
=

关键字“is”比较两个对象的标识(基本上是存储它们的内存位置),而不是值。它不应用于检查是否相等

由于解释器的具体实现,“is”关键字在您的情况下成功了几次-一些数字由编译器存储以便于访问。您不应期望或依赖这种行为。

根据:

运算符
不为
测试对象标识:
x为y
当且仅当
x
y
为同一对象时为真
x不是y
产生反真值


因此,即使x和y是同一类型且相等,它们也可能不满足
is
关系。

也有一个答案,是谁提出的?对于一小部分整数,这是正确的(在CPython中)。不应该依赖这种行为,因为它不能保证在python实现中是相同的。您知道这些数字的范围吗?(我想应该是-5到256)。还有一点可能很重要,那就是实习行为是不能保证的,而且可能在所有python实现中都不相同。@JoelCornett其实不管是哪种,因为正如您所提到的,实习行为依赖于实现。表达式的结果与此无关<代码>is不应用于检查是否相等(永远),因为ints的实习依赖于实现。谢谢!事实上,
is
实际上可以用于某些数字,这让我很惊讶。所以知道有一些被拘留的物品是件好事。@Wang从你的记忆中得出这样的结论,因为你永远不应该使用它,即使它“有效”。