Reference count

Skip Montanaro skip at pobox.com
Mon Aug 11 15:44:33 EDT 2003


    Pablo> I need to keep a object alive even if its reference count if
    Pablo> zero.  Is there any way to increment its reference count using
    Pablo> Python code, not Py_INCREF or there is any way to avoid deletion
    Pablo> of the object?

Sure, maintain an extra reference to it:

    >>> a = 1000
    >>> id(a)
    6864248
    >>> b = a
    >>> id(b)
    6864248
    >>> sys.getrefcount(a)
    3
    >>> sys.getrefcount(b)
    3
    >>> del a
    >>> sys.getrefcount(b)
    2

Skip





More information about the Python-list mailing list