Reference counts

Roeland Rengelink r.b.rigilink at chello.nl
Thu May 24 10:15:26 CEST 2001


Hi,

Helen Dawson wrote:

> 
> The behaviour of getrefcount() seems odd on integers. I'm not
> worried, just curious.
> 
> >>> import sys
> >>> sys.getrefcount(0)
> 1495
> >>> sys.getrefcount(15)
> 70

Somehow, this one surprises me more than the number of references to 0

> >>> sys.getrefcount(12341234)
> 2
> 
> I understand that the objects for numbers from 0 to 100 are shared,
> but I find it unlikely that there are actually 1495 references to zero,
> and 70 to 15. Zero is a popular number, but 1495? These results
> are typical for absolutely nothing happening in Python
> 

Apparently a lot _has_ happened before nothing _is_ happening ;)

> The refcount for 12341234 looks perfect. It is the minimum value
> allowed for by the getrefcount docs, shown here:
> 
>    >>> print sys.getrefcount.__doc__
>    getrefcount(object) -> integer
> 
>    Return the current reference count for the object.  This includes the
> 
>    temporary reference in the argument list, so it is at least 2.
> 
> However, the problem with that is that getrefcount() for any negative
> integer returns 1!
> 
> >>> sys.getrefcount(-2)
> 1
> 

I'm half guessing, but I think:

-2 is not a literal, but an expression applying the unary minus operator
to the integer literal 2. You pas the result of this expression to the
getrefcount() function. Hence, the only reference to this object is the
local ref in getrefcount()


> Any thoughts?
> 
> Bruce/Helen Dawson

Hope this helps,


Roeland Rengelink
-- 
r.b.rigilink at chello.nl

"Half of what I say is nonsense. Unfortunately I don't know which half"



More information about the Python-list mailing list