The current implementation keeps an array of integer objects for all integers between
-5
and256
, 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 of1
. I suspect the behaviour of Python in this case is undefined. :-)
所以以下行为是正常的。在
>>> a = 256
>>> b = 256
>>> a is b
True
>>> c = 257
>>> d = 257
>>> c is d
False
但是当我声明这样的两个变量时,我得到了真的-
^{pr2}$我已经检查了e和f引用的对象的身份-
>>> id(e)
43054020
>>> id(f)
43054020
它们是一样的。在
我的问题是当我们用分号分隔来声明e和f时会发生什么?为什么它们引用同一个对象(尽管这些值超出了Python的整数对象数组的范围)?在
如果你能像对初学者那样解释它,那就更好了。在
根据Python Data model这是一个意外的行为,这是一个实现细节:
相关问题 更多 >
编程相关推荐