Object Reference?

Chris S. chrisks at NOSPAMudel.edu
Fri Aug 6 21:38:39 EDT 2004


Gandalf wrote:

> Yes, Python figures out when to delete an object, based on its reference 
> count. But Python only counts the references. It keeps track of the 
> number of references for the object. It does not know where the 
> reference is located in memory. The short answer is that you cannot get 
> this information effectively.

The gc module has methods that list an object's referrers and referents. 
Although you're correct in that we're unable to access the total number 
of references since duplicate references are not counted. For instance

import gc
a=[1,2,3]
b=[a,a,a,a]
print gc.get_referrers(a)

would only print out one instance of b, even though it references 'a' 
four times. However, this only seems to apply for direct references, so 
the total number of references can be determined by scanning all 
referrers and counting the duplicate ids.



More information about the Python-list mailing list