[Tutor] Is there Python code for accessing an object's reference count?
Dick Moores
rdm at rcblue.com
Tue Oct 3 13:21:23 CEST 2006
At 03:25 AM 10/3/2006, Andre Roberge wrote:
>On 10/3/06, Dick Moores <rdm at rcblue.com> wrote:
> > Is there Python code for accessing an object's reference count?
>
>See sys.getrefcount.
Fascinating. And surprising.
>>> from sys import getrefcount
>>> getrefcount(1)
493
>>> getrefcount(2)
157
>>> getrefcount(3)
60
>>> getrefcount(22)
12
>>> getrefcount(222)
3
>>> getrefcount(222234523452345345)
2
>>> getrefcount(2.23452345345)
2
>>>
That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed
python, called python, and tried it again. Essentially no change.
When would this go to zero? Only when I shut off my computer?
I just answered that last question by shutting off my computer, and found
>>> from sys import getrefcount
>>> getrefcount(1)
493
>>> q = 1
>>> getrefcount(1)
499
>>> w = 1
>>> getrefcount(1)
500
>>> del w
>>> getrefcount(1)
499
>>> q = 2
>>> getrefcount(1)
498
>>>
Still 493. Why?
Then after "q = 1", 499, not 494. Why?
The other refcounts, after w=1, del w, and q=2, are as I expected.
Thanks,
Dick
More information about the Tutor
mailing list