Re: refcount

Discussion in 'Python' started by Christian Heimes, Jan 29, 2008.

  1. Simon Pickles wrote:
    > Hi,
    >
    > Is is possible to access the refcount for an object?
    >
    > Ideally, I am looking to see if I have a refcount of 1 before calling del


    Help on built-in function getrefcount in module sys:

    getrefcount(...)
    getrefcount(object) -> integer

    Return the reference count of object. The count returned is generally
    one higher than you might expect, because it includes the (temporary)
    reference as an argument to getrefcount().

    Christian
    Christian Heimes, Jan 29, 2008
    #1
    1. Advertising

  2. Christian Heimes

    Benjamin Guest

    On Jan 29, 5:46 am, Christian Heimes <> wrote:
    > Simon Pickles wrote:
    > > Hi,

    >
    > > Is is possible to access the refcount for an object?

    >
    > > Ideally, I am looking to see if I have a refcount of 1 before calling del

    >
    > Help on built-in function getrefcount in module sys:
    >
    > getrefcount(...)
    > getrefcount(object) -> integer
    >
    > Return the reference count of object. 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?
    >
    > Christian
    Benjamin, Jan 29, 2008
    #2
    1. Advertising

  3. Christian Heimes

    Mel Guest

    Benjamin wrote:
    > On Jan 29, 5:46 am, Christian Heimes <> wrote:
    >> Simon Pickles wrote:
    >>> Hi,
    >>> Is is possible to access the refcount for an object?
    >>> Ideally, I am looking to see if I have a refcount of 1 before calling del

    >> Help on built-in function getrefcount in module sys:
    >>
    >> getrefcount(...)
    >> getrefcount(object) -> integer
    >>
    >> Return the reference count of object. 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?


    Well, as long as the object is named "object" in sys.getrefcount's
    namespace, there's at least that one reference to it...

    Mel.
    Mel, Jan 29, 2008
    #3
  4. Benjamin <> 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 -- -- 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
    Sion Arrowsmith, Jan 30, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ames Andreas (MPA/DF)

    [Q] Extension: Refcount for exception types

    Ames Andreas (MPA/DF), Apr 16, 2004, in forum: Python
    Replies:
    1
    Views:
    277
    =?windows-1252?Q?=22Martin_v=2E_L=F6wis=22?=
    Apr 16, 2004
  2. Jean-François Doyon

    Debugging leaking refcount

    Jean-François Doyon, May 14, 2004, in forum: Python
    Replies:
    0
    Views:
    288
    Jean-François Doyon
    May 14, 2004
  3. Jens Theisen

    beginner's refcount questions

    Jens Theisen, Oct 30, 2006, in forum: Python
    Replies:
    3
    Views:
    259
    Fredrik Lundh
    Oct 30, 2006
  4. Gabriel Genellina

    refcount differences in 2.5

    Gabriel Genellina, May 17, 2007, in forum: Python
    Replies:
    0
    Views:
    244
    Gabriel Genellina
    May 17, 2007
  5. Replies:
    1
    Views:
    355
    Gabriel Genellina
    Nov 1, 2007
Loading...

Share This Page