counting references to an instance

Aahz aahz at
Wed Feb 4 22:22:26 CET 2004

In article <pan.2004. at>,
Brian  <balex at> wrote:
>On Wed, 04 Feb 2004 14:57:59 -0500, Aahz wrote:
>> In article <pan.2004. at>, Brian
>> <balex at> wrote:
>>>Is it possible to get an object to return the number of references there
>>>are to itself?
>>>class test(object):
>>> pass
>>>a = test()
>>>b = a
>>># Should print "2", if I knew the name of the method. print a.refCount()
>> sys.getrefcount(a) will do the trick -- but it will print 3.  Can you
>> figure out why?
>Does the instance of test contain a reference to itself?

Nope.  Robert Brewer's response explained it.
Aahz (aahz at           <*>

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code -- 
not in reams of trivial code that bores the reader to death."  --GvR

More information about the Python-list mailing list