Reference counts

Mike C. Fletcher mcfletch at home.com
Thu May 24 04:13:05 EDT 2001


S:\worldbuilder1.0\dist>python
Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getrefcount( 0)
48
>>> sys.getrefcount( 15)
5
>>> sys.getrefcount( -2)
1
>>> value = -2
>>> sys.getrefcount( -2)
1
>>> sys.getrefcount( value)
2
>>> -2 is value
0
>>>

That is, -2 is a negative unary operator, followed by a 2, it results in a
value which is the integer -2, which isn't shared (see "-2 is value"), and
isn't part of the argument list (which is the result of a unary operation on
the shared object 2), hence the only reference to the particular object is
inside the getrefcount function.

As for the high 0 counts, every module where 0 is a default value is going
to increase the ref count, so in systems where huge numbers of modules are
imported (such as PythonWin or IDLE), you'll see a very high count compared
to the simple interpreter:

PythonWin 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2000 Mark Hammond (MarkH at ActiveState.com) - see
'Help/About PythonWin' for further copyright information.
>>> import sys
>>> sys.getrefcount( 0)
1108
>>>


Enjoy,
Mike


-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Helen Dawson
Sent: May 24, 2001 02:55
To: python-list at python.org
Subject: Reference counts


The behaviour of getrefcount() seems odd on integers. I'm not
worried, just curious.

>>> import sys
>>> sys.getrefcount(0)
1495
>>> sys.getrefcount(15)
70
>>> 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

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

Any thoughts?

Bruce/Helen Dawson


--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list