python delivery method
Variable, reference, object
1. Variable refers to the object through the variable pointer. The variable pointer points to the memory space of the specific object and takes the value of the object.
2. Object, the type is known, and each object contains a header information (header information: type identifier and reference counter)
3. The variable name has no type. The type belongs to the object (because the variable references the object, the type follows the object). The variable refers to what type of object, and the variable is of what type.
Mutable and immutable objects
Python allocates objects in heap into two categories: variable objects and immutable objects. The so-called variable object means that the content of the object is variable, such as list. An immutable object, on the contrary, indicates that its content is immutable.
Immutable object: int,string,float,tuple -- Can be understood as C In, this parameter is passed as a value Variable object: list,dictionary -- Can be understood as C In, this parameter is passed as a pointer
For immutable objects,
In fact, it is unique in memory. Different variable names are just different references, pointing to the same memory;
For mutable objects,
Even if the data in the memory is the same, it will reopen a new memory to store the same content.
For example:
l1=[1,2,3] l2=[1,2,3] a=1 b=1 print(id(l1)) print(id(l2)) print(id(a)) print(id(b)) # The id() function returns the unique identifier of the object, which is an integer. It can be understood that the id() function is used to obtain the memory address of the object.
mem add: 10943008 reass mem add: 10943008 reass mem add: 10943040 mem add: 10943008
in other words:
1. Python caches immutable objects such as integers and short strings, so each object has only one copy in memory, and the object referred to by reference is the same. Even if an assignment statement is used, it only creates a new reference, not the object itself;
2. Python does not cache long strings, lists and other objects. These variable objects can be created from multiple identical objects, and new objects can be created using assignment statements.
3. In python, immutable objects are shared, and creating a variable object is always to assign a new address
Value passing and reference passing
1. Value transfer
def reassign(int_a): print('start fun mem add: {}'.format(id(int_a))) int_a = 2 print('reass mem add: {}'.format(id(int_a))) return None if __name__ == '__main__': int_a = 1 print('mem add: {}'.format(id(int_a))) reassign(int_a) print('mem add: {}'.format(id(int_a))) print(int_a)
mem add: 10943008 reass mem add: 10943008 reass mem add: 10943040 mem add: 10943008
def reassign(list_a): print('start fun mem add: {}'.format(id(list_a))) list_a = [2] print('reass mem add: {}'.format(id(list_a))) return None if __name__ == '__main__': list_a = [1] print('mem add: {}'.format(id(list_a))) reassign(list_a) print('mem add: {}'.format(id(list_a))) print(list_a)
mem add: 139900071267144 start fun mem add: 139900071267144 reass mem add: 139900071267208 mem add: 139900071267144 [1]
2. Reference passing
def append(list_a): print('start fun mem add: {}'.format(id(list_a))) list_a.append(2) print('reass mem add: {}'.format(id(list_a))) return None if __name__ == '__main__': list_a = [1] print('mem add: {}'.format(id(list_a))) append(list_a) print('mem add: {}'.format(id(list_a))) print(list_a)
mem add: 139775496346376 start fun mem add: 139775496346376 reass mem add: 139775496346376 mem add: 139775496346376 [1, 2]