fen*_*gsp 4 python python-internals
在Python 2.7.5下
>>> import sys
>>> sys.getrefcount(10000)
3
Run Code Online (Sandbox Code Playgroud)
这三个引用计数在哪里?
PS:当10000 PyIntObject将Py_DECREF改为0 ref并解除分配时?不要说gc的东西,引用计数本身可以在没有gc的情况下工作.
当你在REPL控制台中执行某些操作时,字符串将在内部编译,在编译过程中,Python会创建一个中间列表,其中包含除标记之外的字符串列表.所以,这是参考编号1.您可以这样检查
import gc
print gc.get_referrers(10000)
# [['sys', 'dis', 'gc', 'gc', 'get_referrers', 10000], (-1, None, 10000)]
Run Code Online (Sandbox Code Playgroud)由于它只是一个数字,在编译过程中,Python的窥视孔优化器将数字存储为生成的字节码中的常量之一.你可以这样检查一下
print compile("sys.getrefcount(10000)", "<string>", "eval").co_consts
# (10000,)
Run Code Online (Sandbox Code Playgroud)注意:
Python在列表中存储10000的中间步骤仅适用于编译的字符串.这不是为已编译的代码生成的.
print eval("sys.getrefcount(10000)")
# 3
print eval(compile("sys.getrefcount(10000)", "<string>", "eval"))
# 2
Run Code Online (Sandbox Code Playgroud)
在第二个示例中,我们使用compile函数编译代码,并仅将代码对象传递给eval函数.现在只有两个参考.一个来自窥视孔优化器创建的常量,另一个是来自sys.getrefcount.
| 归档时间: |
|
| 查看次数: |
348 次 |
| 最近记录: |