Home > front end >  Why does my Python Extension type have two ref counts rather than one?
Why does my Python Extension type have two ref counts rather than one?

Time:09-30

I'm maintaining a Python type that has been built as a Python extension using the Python C API. My question is regarding the lifetime of this "NamedArray" object. Essentially my test code looks like this:

    def test_init_from_constructor(self):
        """

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

My issue is that the ref count of the newly instantiated NamedArray object is 2, yet I expect it to be 1. Where is the other reference coming from ?

CodePudding user response:

This is documented under sys.getrefcount

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()

  • Related