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

Cia*_*lsh 1 c python refcounting python-c-api

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

    def test_init_from_constructor(self):
        """

        :return:
        """
        n = NamedArray((2, 3))
        self.assertIsInstance(n, NamedArray)
        self.assertEqual(2, sys.getrefcount(n))
Run Code Online (Sandbox Code Playgroud)

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

Abd*_*P M 5

这记录在sys.getrefcount

sys.getrefcount(object)

返回对象的引用计数。返回的计数通常比您预期的高 1,因为它包含(临时)引用作为 getrefcount() 的参数