refcount

Duncan Booth duncan.booth at invalid.invalid
Tue Jan 29 06:41:47 EST 2008


Simon Pickles <sipickles at hotmail.com> wrote:
> Is is possible to access the refcount for an object?

>>> import sys
>>> sys.getrefcount(42)
6

> Ideally, I am looking to see if I have a refcount of 1 before calling del

That's a pointless exercise: you probably don't understand what del does.

All that del does is remove one reference from an object, either by 
removing a name from the namespace, or by removing a reference from 
something like a list. 'del x' does NOT destroy the object referenced by x, 
unless it happens that there are no other references to the object.

Also note that the only time you will see a reference count of 1 is on an 
object which you cannot otherwise access (the only reference is being used 
for the call to getrefcount()):

>>> x = 99999
>>> sys.getrefcount(x)
2



More information about the Python-list mailing list