"Python" object references and garbage collection

  • Alias: When an object is referenced multiple variables, the extra variables object called an alias.
  • Each variable has identification, type, and value.
  • Once an object is created to have a logo, the logo will not change within the lifetime of the object.
  • Built-in id()functions return an object identifier, isidentifier operator compares two objects.

An object can be referenced by a plurality of variables; stored identification variable for object identification, object data variable has a value.

In the memory address to identify objects in their Cpython of other Python implementations may be other values.

  • ==And is: isoperators than ==fast, because it can not reload, Python without having to find and call a special method. ==Specific achieve special visual data comparison method is determined.

  • Immutability immutable sequence are relative, i.e. the element which is variable, identifying each element but does not change.


First, the object reference

1. Copy the deep and shallow copy

Copy the relative depth of the container with reference to the element concerned . Simply speaking, come shallow copy of a copy of the elements within it a direct copy of the source element that contains the reference, thus causing the two container elements of shared references; and deep copy of a copy to the re-created object as an element. The only problem shared references brought on mutable objects occur that bring light trap conditions copied: Object variable contains an object element.

a = (1,2,3,[4,5,6])
b = a[:]

for i in range(7,10):
	a[3].append(i)

b
>> (1,2,3,[4,5,6,7,8,9])

 Also note that changes to an object in the shallow copy will not necessarily be reflected in another object, only the object in place of the variable element changes to be reflected to another object .

a = [1,2,3,[]]
b = list(a)

a[3].append(4)
b
>> [1,2,3,[4]]

a.append(5)
b
>> [1,2,3,[4]]

a[3] = [44,55,66]
b
>> [1,2,3,[4]]

Python is the default shallow copy replication (sliced, constructor).

Note distinguish the difference between replication and variable assignment, to create a replica of the object.

Deep and shallow copy copy

copyModule provided deepcopywith copyfunctions which provide deep copy and shallow copy of any object. And the deepcopy()function will remember the replicated objects, it is possible to deal with circular references gracefully.

2. function as a reference parameter

 When the function parameter mutable objects may cause the following problems:

Ⅰ. REFERENCE transfer function parameters

 Python only supported parameter passing mode is a shared transmission parameters, i.e., the function parameter arguments are aliases. Problems caused by this way, the internal function may modify the parameters passed in place of the variable objects, resulting in a change outside the function of the original object.

Ⅱ. Do not use a variable type as the default function of the parameters

 You should avoid using a variable object as the default value of the parameter. Problem caused by the parameter default values ​​when defining the function calculation (typically when the loading module), the default value as a function of object properties. At this time, if the default value of the variable objects, and in vivo function of the modified value, causes subsequent function calls affected. That is, multiple calls to the function will use the same variable object default parameters.

Second, garbage collection

 Python can use the delstatement to delete delete the name (variable), but delcan not be directly deleted objects. Only when delremoved the last reference to the object, it will cause the garbage collection.

Python, there is a special way __del__, it calls upon the object instance is automatically called by the interpreter, the last chance to release an example of external resources. I have written code that rarely need to achieve __del__.


 In CPython, the main garbage collection algorithms is the reference count. When the reference count to zero, the object is immediately destroyed. While garbage collection CPython there generational garbage collection algorithm, if all references to each other between a group of objects, so that the object can not be obtained in the array, this time can cause the garbage collector.

1. Weak references

 Weak reference does not increase the number of reference object, i.e., it does not interfere with the garbage collection object. Weak references commonly used in the cache, we need to save object reference, but do not want to be cached due reference is always present.

发布了51 篇原创文章 · 获赞 1 · 访问量 1313

Guess you like

Origin blog.csdn.net/weixin_44471152/article/details/104317037