watching the number of references to an object

Michael Hudson mwh at python.net
Thu Feb 14 11:02:45 EST 2002


"Paul Brian" <paul1brian at yahoo.com> writes:

> Dear all,
> 
> Does anyone know how to monitor the references pointing to an instance of an
> object?
> 
> the gc module (according to docs) seems to show those with no
> references left, but I am interested in seeing how many refs a given
> instance has...any ideas on back of a postcard please

You want gc.get_referrers (new in 2.2, I think), or maybe just
sys.getrefcount.

>>> import gc
>>> len(gc.get_referrers(gc))
2

(the referrers are going to be sys.modules & __main__.__dict__ here).

Cheers,
M.

-- 
  at any rate, I'm satisfied that not only do they know which end of
  the pointy thing to hold, but where to poke it for maximum effect.
                                  -- Eric The Read, asr, on google.com



More information about the Python-list mailing list