Lets say you wrote a C++ (or C) extension to python, called module
. It returns an array of arrays. What should the returned array, and its arrays, have as reference counts?
The python code would be something like
import my_module
from sys import getrefcount as g # will use to check reference count
def use_module():
x = my_module.func()
print 'x =', x
print 'g(x) =', g(x)
print 'g(x[0]) =', g(x[0])
test = 'some random thing'
print 'should be', g(test), '?'
use_module()
>>> x = ( (1,2,3,4) , [2,3] , {'one':1} )
>>> g(x) = 3
>>> g(x[0]) = 3
>>> should be 2 ?
I would expect g(x)
to be 2
, not 3
. (after adding one for g
to reference x
)
In my C++ extension, I have made sure that the array and all its sub-collections and their elements have reference counts of 1
before being returned to python, so I'm not sure how it shot up to 3
so quickly? Perhaps returned PyObject*
's should have 0
as its reference count?
Edit: sorry, I'm an idiot. I was making another reference without knowing it.