The case where all Python object reference counts are reduced by 1:
I. The alias of the object is assigned a new object;
A = 23345455 # added a reference to B = a # added a reference to print= 1.4 # reduced the reference to a 23345455 integer print(Sys.getrefcount (a))
Results: 3;2
Two. The alias of the object is explicitly destroyed;
A = 23345455 # adds a reference to B = a # adds a reference to list = [A, b] # Added 2 references del aprint(Sys.getrefcount (b))
Results: 4
Description: Use the DEL keyword or the Del () function directly; Note: The above code is manually destroyed by the assigned reference to a, but in the list of a will not be destroyed.
Three. An object leaves its scope;
A = 23345455 # adds a reference to B = a # adds a reference to print(Sys.getrefcount (a)) # after execution, reference to destroy print(Sys.getrefcount (a))
Results: 3;3
Note:A is passed as a parameter to the Sys.getrefcount (a) function, only works in the function and is destroyed once execution is complete.
Four. The container in which the object is located is destroyed, or the object is deleted from the container;
# added a reference to a = 23345455# adds a reference to B == [A, b] # added 2 references to del listprint(Sys.getrefcount (b))
Results: 3
All reference counts plus one: python's reference count Analysis (i)
Python's reference count Analysis (ii)