为什么 Python Ref Counts 到小整数出奇的高? [英] Why are Python Ref Counts to small integers surprisingly high?

查看:35
本文介绍了为什么 Python Ref Counts 到小整数出奇的高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

在第一次 sys.getrefcount 调用时,这个对象恰好有 20 个对它的引用.这不仅仅是您创建的参考;在其他模块和 Python 内部有各种各样的其他引用,因为(这是一个实现细节)Python 的标准实现只创建一个 100 对象并将它用于所有出现的100 在 Python 程序中.

In this answer I found a way to get a reference count of objects in Python.

They mentioned using sys.getrefcount(). I tried it, but I'm getting an unexpected result. When there is 1 reference, it seems as though the count is 20. Why is that?

I looked at the documentation but it doesn't seem to explain the reason.

解决方案

This object happens to have 20 references to it at the time of the first sys.getrefcount call. It's not just the references you created; there are all sorts of other references to it in other modules and in the Python internals, since (this is an implementation detail) the standard implementation of Python only creates one 100 object and uses it for all occurrences of 100 in a Python program.

这篇关于为什么 Python Ref Counts 到小整数出奇的高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆