Is the delivery of Python a value pass or a reference pass?
Before you answer this question, you need to know that a variable in Python is simply a reference to an object. The assignment does not change what the object points to, but instead changes the variable from one object's reference to the reference to another object. After copying a variable, the ID of the variable before and after the copy changes. In the case of variable direct operation, the variable object (list dict ... ) and the immutable object (int str tuple ... )。 The former does not change, while the latter changes, because the original cannot be changed, only a new value is returned and assigned to the variable.
In [1]: i = 1In [2]: ID (i) out[2]: 31122120LIn [3]: i = 5In [4]: ID (i) out[4]: 31122024LIn [5]: a = [A.]in [6]: ID (a) out[6]: 62749960LIn [7]: a = [23]in [8]: ID (a) out[8]: 62757576LIn [9]: i = 5In [10]: ID (i) out[Ten]: 31122024LIn [ALL]: i + = 1In [12]: ID (i) out[]: 31122000LIn [13]: ID (a) out[]: 62757576LIn [+]: A.append (5) in [15]: ID (a) out[]: 62757576L
An assignment cannot change an object that refers to an address within a function or outside a function. So a python function is passed by reference, passing a reference to the object. As for whether or not to modify this parameter, it depends on the operation of the variable and whether the variable is mutable or immutable.
deff (n,x): N= 3X.append (7) #x = [4,5,6] Print "In F ()", N,xif __name__=="__main__": N= 1x= [0,1,2] Print "Before:", N,x F (n,x)Print "After :", N,x>>>Before:1 [0, 1, 2]in F ()3 [0, 1, 2, 7]after:1 [0, 1, 2, 7]deff (n,x): N+ = 3X.append (7) #x = [4,5,6] Print "In F ()", N,xif __name__=="__main__": N= 1x= [0,1,2] Print "Before:", N,x F (n,x)Print "After :", N,xbefore:1 [0, 1, 2]in F ()4 [0, 1, 2, 7]after:1 [0, 1, 2, 7]deff (n,x): N= 3#X.append (7)x = [4,5,6] Print "In F ()", N,xif __name__=="__main__": N= 1x= [0,1,2] Print "Before:", N,x F (n,x)Print "After :", N,xbefore:1 [0, 1, 2]in F ()3 [4, 5, 6]after:1 [0, 1, 2]
Python function Parameter problems