[Tutor] Python cashes low integers? How? Where?

Dick Moores rdm at rcblue.com
Mon Aug 9 10:22:01 CEST 2004


Kalle Svensson wrote at 00:59 8/9/2004:
>[Dick Moores]
> > If I could ask what seems to be a related question: On p. 85 Chun
> > says, "Each object has associated with it a counter that tracks the
> > total number of references that exist to that object. This number
> > simply indicates how many variables are 'pointing to' any particular
> > object."  However, he doesn't say how to find this number. Is there
> > a way?
>
>Yes.  The number is called a refcount, and can be read with the
>function sys.getrefcount.
>http://python.org/doc/lib/module-sys.html#l2h-344

Thanks!

But why do I get these results for sys.getrefcount(3456)? Shouldn't it 
return at least a 3, and then drop to one less after  del a  ?

 >>>import sys
 >>> a = b = c = 3456
 >>> sys.getrefcount(3456)
2
 >>> a is b
True
 >>> b is c
True
 >>> a is c
True
 >>> del a
 >>> sys.getrefcount(3456)
2

With "Dick" I get pretty much what I expected:

a = b = c = "Dick"
 >>> sys.getrefcount("Dick")
5
 >>> del a
 >>> sys.getrefcount("Dick")
4

Dick



More information about the Tutor mailing list