Talk about the type of the parameter list and copy of python

Because of the special mechanism of the python, the use of types of parameters in the function list, the argument is actually participating in the virtual address passed through, i.e., modify the value of the dummy argument, actually it changes the value of the argument, is very dangerous, so try Do not modify the value of the imaginary argument.

In the application it should also be noted, can not be cut off to modify the dummy argument must be assigned (the so-called deep copy) by way of re-building list by assigning an equal sign (so-called shallow copy) mode.

= X1 [1,2 ] 
Y1 = X1   # shallow copy, 
Print (ID (X1), ID (Y1))    # At this time x1, y1 same address 
y1.append (0)            # At this time x1, y1 are [1 , 2,0] 
Y1 = [I for I in X1]      # deep copy, using the push-type list, it is convenient 
Print (ID (X1), ID (Y1))    # At this time x1, y1 same address different 
y1.append (0)            # case x1 is [1,2], y1 is [1,2,0]

 

Guess you like

Origin www.cnblogs.com/imhuanxi/p/11222878.html