为什么我的Python扩展类型有两个ref计数而不是一个?

2024-10-01 15:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在维护一个Python类型,它是使用pythoncapi作为Python扩展构建的。我的问题是关于这个“NamedArray”对象的生命周期。基本上,我的测试代码如下所示:

    def test_init_from_constructor(self):
        """

        :return:
        """
        n = NamedArray((2, 3))
        self.assertIsInstance(n, NamedArray)
        self.assertEqual(2, sys.getrefcount(n))

我的问题是新实例化的NamedArray对象的ref计数是2,但我希望它是1。另一个参考来自哪里


Tags: 对象fromtestself类型returninitdef
1条回答
网友
1楼 · 发布于 2024-10-01 15:30:02

这记录在^{}

sys.getrefcount(object)

Return the reference count of the object. The count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount()

相关问题 更多 >

    热门问题