refcount

Sion Arrowsmith siona at chiark.greenend.org.uk
Wed Jan 30 08:03:08 EST 2008


Benjamin  <musiccomposition at gmail.com> wrote:
>> [ help(sys.getrefcount) says: ]
>> [ ... ]  The count returned is generally
>> one higher than you might expect, because it includes the (temporary)
>> reference as an argument to getrefcount().
>Are there any cases when it wouldn't?

When the temporary reference which is the argument to getrefcount is
the *only* reference, eg:

>>> sys.getrefcount (set())
1

The return value for a weakly referenced object may also be not what
you "expect":

>>> s = set()
>>> sys.getrefcount(s)
2
>>> r = weakref.ref(s)
>>> r() is s
True
>>> sys.getrefcount(r())
2

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
        -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list