ipython 和引用计数
看来 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
发生了什么?额外的两个参考文献是从哪里来的?
It would appear that ipython
may be retaining references to objects when I'm not expecting it to.
Consider the following script (grc.py
):
import sys
obj = []
print sys.getrefcount(obj)
When I run it in 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
What's going on? Where did the extra two references come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,IPython 确实在幕后维护对对象的引用。特别是使用 %run 在处理模块的机器中留下引用。
我们正在开发版本中解决这个问题(希望很快成为 0.11) - 请参阅此问题< /a> 了解更多信息。引用仍然存在,但有多种方法可以消除它们(
%reset
可以清除所有引用,或者%xdel obj
可以清除单个引用)。Yes, IPython does maintain references to objects behind the scenes. Using %run in particular leaves references in the machinery that handles modules.
We're working on it in the development version (which will hopefully become 0.11 soon) - see this issue for more information. The references will still be there, but there'll be ways to get rid of them (
%reset
to clear them all, or%xdel obj
for a single one).