counting references to an instance

Aahz aahz at
Wed Feb 4 20:57:59 CET 2004

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?
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