Jam*_*pam 8 python cpython python-2.7
从这个链接我了解到了
当前实现为-5到256之间的所有整数保留一个整数对象数组,当您在该范围内创建一个int时,实际上只返回对现有对象的引用
但是当我尝试为我的会话提供一些示例时,我发现它在赋值和元组解包时表现不同.
这是片段,
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a,b = 300,300
>>> a is b
True
>>> c = 300
>>> d = 300
>>> c is d
False
>>>
Run Code Online (Sandbox Code Playgroud)
因为int是不可变的,所以Python可能会也可能不会使用exists对象,如果将以下代码保存到脚本文件中,并运行它,它将输出两个True.
a, b = 300, 300
print a is b
c = 300
d = 300
print c is d
Run Code Online (Sandbox Code Playgroud)
当Python编译代码时,它可能会重用所有常量.假设您在python会话中输入代码,代码是逐行编译的,Python不能将所有常量重用为一个对象.
该文档仅表示将只有一个实例为-5到256,但不定义其他实例.对于稳定的类型,is而is not不是重要的,因为你无法修改它们.
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |