“是"运算符对整数的行为异常

问题描述

为什么在 Python 中出现以下异常行为?

Why does the following behave unexpectedly in Python?

>>> a = 256
>>> b = 256
>>> a is b
True           # This is an expected result
>>> a = 257
>>> b = 257
>>> a is b
False          # What happened here? Why is this False?
>>> 257 is 257
True           # Yet the literal numbers compare properly

我使用的是 Python 2.5.2.尝试一些不同版本的 Python,似乎 Python 2.3.3 显示了 99 到 100 之间的上述行为.

I am using Python 2.5.2. Trying some different versions of Python, it appears that Python 2.3.3 shows the above behaviour between 99 and 100.

基于上述,我可以假设 Python 是在内部实现的,因此小"整数的存储方式与较大的整数不同,is 运算符可以区分.为什么有泄漏的抽象?当我事先不知道它们是否是数字时,比较两个任意对象以查看它们是否相同,有什么更好的方法?

Based on the above, I can hypothesize that Python is internally implemented such that "small" integers are stored in a different way than larger integers and the is operator can tell the difference. Why the leaky abstraction? What is a better way of comparing two arbitrary objects to see whether they are the same when I don't know in advance whether they are numbers or not?


解决方案

看看这个:

>>> a = 256
>>> b = 256
>>> id(a)
9987148
>>> id(b)
9987148
>>> a = 257
>>> b = 257
>>> id(a)
11662816
>>> id(b)
11662828

这是我在 Python 2 文档中找到的内容,"Plain Integer Objects"(对于 Python 3 也是一样):

Here's what I found in the Python 2 documentation, "Plain Integer Objects" (It's the same for Python 3):

当前的实现保留了一个所有人的整数对象数组-5 到 256 之间的整数,当你在该范围内创建一个 int实际上只是取回参考现有的对象.所以应该是可以改变 1 的值.我怀疑 Python 在这种情况是未定义的.:-)

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)

相关文章