“是"运算符对整数的行为出乎意料 [英] "is" operator behaves unexpectedly with integers
问题描述
为什么在 Python 中以下行为会出乎意料?
<预><代码>>>>一 = 256>>>乙 = 256>>>a 是 bTrue # 这是预期的结果>>>一 = 257>>>乙 = 257>>>a 是 b错误 # 这里发生了什么?为什么这是假的?>>>257 是 257True # 然而文字数字比较正确我使用的是 Python 2.5.2.尝试一些不同版本的 Python,似乎 Python 2.3.3 在 99 和 100 之间显示了上述行为.
基于上述内容,我可以假设 Python 是在内部实现的,因此小"整数的存储方式与大整数不同,并且 is
运算符可以分辨出差异.为什么有泄漏的抽象?当我事先不知道它们是否是数字时,比较两个任意对象以查看它们是否相同的更好方法是什么?
看看这个:
<预><代码>>>>一 = 256>>>乙 = 256>>>身份证(一)9987148>>>编号(b)9987148>>>一 = 257>>>乙 = 257>>>身份证(一)11662816>>>编号(b)11662828这是我在 Python 2 文档中找到的内容,"Plain Integer Objects"(Python 3 也是如此):
<块引用>当前的实现保持所有整数对象的数组-5 到 256 之间的整数,当你在该范围内创建一个 int 你实际上只是取回对现有的对象.所以应该是可以改变 1 的值.我怀疑 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
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.
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?
Take a look at this:
>>> a = 256
>>> b = 256
>>> id(a)
9987148
>>> id(b)
9987148
>>> a = 257
>>> b = 257
>>> id(a)
11662816
>>> id(b)
11662828
Here's what I found in the Python 2 documentation, "Plain Integer Objects" (It's the same for Python 3):
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. :-)
这篇关于“是"运算符对整数的行为出乎意料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!