A few important points are not very familiar.
Reference count and garbage collection
1. All objects are referenced and counted. When a new name is assigned to an object or is put into a container (list, tuples, or dictionary), the reference count of this object increases by 1.
A = 1 # assign a new name, and increase the reference count of object 1 by 1
B = A # assign a new name. The reference count of object 1 is increased by 1 or B = 1 A. B is a reference to the same object.
C = []
C. append (a) # increase the reference count by 1 or C. append (1 ).
Differentiate what is an object and what is an object reference. The reference name of an object is also an object. In python, everything is an object.
2 del statement, out of the scope of the variable, or the variable is redefined, will cause the reference count to be reduced by 1.
Del
B = 2
C [0] = 2
3. When the reference count of an object changes to 0, the object will be reclaimed by the garbage collector at an appropriate time.
However, when mutual reference occurs, it seems that the count of the two objects will never change to 0. although neither object can be accessed by name, the counter is not reduced to 0. in this case, the python interpreter regularly runs a searcher to clear inaccessible objects.
Binary reference and copy
1. For a mutable object, if one of its references is changed, all its references are changed.
A = [1, 2, 3]
B =
A [0] =-1 # B = [-1, 2, 3]
A. append (4) # B = [-1, 2, 4]
2. For immutable objects, changing a reference of an object is actually creating a new object, which has no effect on other references.
A = (1, 2, 3)
B =
B = [1, 2, 3]
In this case, a is a and B is B.
3. Shallow copy and deep copy
A new object is created for the shortest copy, but its child elements are still referenced by the child elements of the original object.
Understanding
1) copy and create a new object
A = [1, 2, 4]
B = A [:] # the elements in a are copied in the shortest copy, but B and A are not references to the same object. B creates a new object.
So
B. append (100) # A will not be affected
2) The child element is still a reference to the child element of the original object.
B [0] =-1 # A will not change. The child element is an immutable object. For more information, see the preceding two points.
B [2] [0] =-3 # A changes. The child element is a mutable object. For more information, see.
Three Tips
Apply ()
Lambda
Map ()
Zip ()
Reduce ()
Filter ()