Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Is there Python code for accessing an object's referencecount?

    6 answers - 305 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Is there Python code for accessing an object's reference count?
    And does it matter to Python what this count is, other than whether
    it is zero or greater than zero (for garbage collection)?
    Thanks,
    **** Moores
    Tutor maillist - Tutor (AT) python (DOT) org
  • No.1 | | 463 bytes | |

    10/3/06, **** Moores <rdm (AT) rcblue (DOT) comwrote:
    Is there Python code for accessing an object's reference count?

    See sys.getrefcount.

    A Roberge

    And does it matter to Python what this count is, other than whether
    it is zero or greater than zero (for garbage collection)?

    Thanks,

    **** Moores

    Tutor maillist - Tutor (AT) python (DOT) org

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.2 | | 1234 bytes | |

    At 03:25 AM 10/3/2006, Andre Roberge wrote:
    10/3/06, **** Moores <rdm (AT) rcblue (DOT) comwrote:
    Is there Python code for accessing an object's reference count?
    >
    >See sys.getrefcount.


    Fascinating. And surprising.
    from sys import getrefcount
    getrefcount(1)
    493
    getrefcount(2)
    157
    getrefcount(3)
    60
    getrefcount(22)
    12
    getrefcount(222)
    3
    getrefcount(222234523452345345)
    2
    getrefcount(2.23452345345)
    2

    That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed
    python, called python, and tried it again. Essentially no change.
    When would this go to zero? when I shut off my computer?

    I just answered that last question by shutting off my computer, and found
    from sys import getrefcount
    getrefcount(1)
    493
    q = 1
    getrefcount(1)
    499
    w = 1
    getrefcount(1)
    500
    del w
    getrefcount(1)
    499
    q = 2
    getrefcount(1)
    498

    Still 493. Why?

    Then after "q = 1", 499, not 494. Why?

    The other refcounts, after w=1, del w, and q=2, are as I expected.

    Thanks,

    ****

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.3 | | 1240 bytes | |

    **** Moores wrote:
    At 03:25 AM 10/3/2006, Andre Roberge wrote:
    >10/3/06, **** Moores <rdm (AT) rcblue (DOT) comwrote:

    Is there Python code for accessing an object's reference count?
    >See sys.getrefcount.


    Fascinating. And surprising.
    from sys import getrefcount
    getrefcount(1)
    493
    getrefcount(2)
    157
    getrefcount(3)
    60
    getrefcount(22)
    12
    getrefcount(222)
    3
    getrefcount(222234523452345345)
    2
    getrefcount(2.23452345345)
    2

    That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed
    python, called python, and tried it again. Essentially no change.
    When would this go to zero? when I shut off my computer?

    When you exit Python.

    There are quite a few modules loaded automatically when Python starts
    up. It's not too surprising that a few of them reference 1. For example,
    defining a function that uses a constant creates a reference to the
    constant:
    In [2]: sys.getrefcount(100)
    [2]: 43

    In [3]: def foo():
    : return 100
    :

    In [4]: sys.getrefcount(100)
    [4]: 44

    Kent

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.4 | | 1464 bytes | |

    At 04:44 AM 10/3/2006, Kent Johnson wrote:
    >**** Moores wrote:

    At 03:25 AM 10/3/2006, Andre Roberge wrote:
    >10/3/06, **** Moores <rdm (AT) rcblue (DOT) comwrote:

    Is there Python code for accessing an object's reference count?
    >See sys.getrefcount.
    >

    Fascinating. And surprising.
    from sys import getrefcount
    getrefcount(1)
    493
    getrefcount(2)
    157
    getrefcount(3)
    60
    getrefcount(22)
    12
    getrefcount(222)
    3
    getrefcount(222234523452345345)
    2
    getrefcount(2.23452345345)
    2

    That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed
    python, called python, and tried it again. Essentially no change.
    When would this go to zero? when I shut off my computer?
    >
    >When you exit Python.
    >
    >There are quite a few modules loaded automatically when Python starts
    >up. It's not too surprising that a few of them reference 1. For example,
    >defining a function that uses a constant creates a reference to the
    >constant:
    >In [2]: sys.getrefcount(100)
    >[2]: 43
    >
    >In [3]: def foo():

    : return 100
    :
    >
    >In [4]: sys.getrefcount(100)
    >[4]: 44


    Ah, got it. Thanks again, Kent.

    ****

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.5 | | 334 bytes | |

    Kent Johnson wrote:

    In [2]: sys.getrefcount(100)
    [2]: 43

    In [3]: def foo():
    : return 100
    :

    In [4]: sys.getrefcount(100)
    [4]: 44

    off-topic:

    how to you get this output format of the shell?

    In [2]:
    [2]:

    I've tried to alter IDLE:

    IDLE - Customizing output format
  • No.6 | | 219 bytes | |

    Ilias Lazaridis wrote:
    how to you get this output format of the shell?
    In [2]:
    [2]:
    IPython
    http://ipython.scipy.org/
    Kent
    Tutor maillist - Tutor (AT) python (DOT) org

Re: Is there Python code for accessing an object's referencecount?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 9 + 8 =
QUESTION ON "Python"

EMSDN.COM