gpt4 book ai didi

python - ipython 和引用计数

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:21 25 4
gpt4 key购买 nike

看起来 ipython 可能在我不期望的时候保留了对对象的引用。

考虑以下脚本 (grc.py):

import sys
obj = []
print sys.getrefcount(obj)

当我在 ipython 中运行它时:

Python 2.7.1 |EPD 7.0-2 (64-bit)| (r271:86832, Nov 29 2010, 13:51:37)

In [1]: %run grc.py
2

In [2]: print sys.getrefcount(obj)
4

这是怎么回事?多出的两个引用从何而来?

最佳答案

是的,IPython 确实在幕后维护对对象的引用。特别是使用 %run 会在处理模块的机器中留下引用。

我们正在开发版本中对其进行处理(希望很快成为 0.11)- 参见 this issue了解更多信息。引用仍会存在,但有办法摆脱它们(%reset 清除所有它们,或 %xdel obj 清除单个引用)。

关于python - ipython 和引用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6019835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com