Transfer function parameters python

 The only parameter passing mode Python support is "shared mass participation" (call by sharing) Most object-oriented languages ​​have adopted this model, including Ruby, Smalltalk and Java (Java reference type is a basic type of pass by value) share transfer reference means in the form of various parameters of the function of obtaining a copy of each argument a reference; that is, the internal parameter function argument is an alias (alias) the result of this approach is that the function may be passed as a parameter to modify mutable objects, but can not be modified to identify those objects (ie, can not replace one object to another object.

 1  >>> def fun(num,l,d):
 2 ...     num=123;
 3 ...     l=[1,2,3]
 4 ...     d={'a':123}
 5 ...     print("inside:","num=%f,l=%s,d=%s"%(num,l,d))
 6 ... 
 7 >>> num=1
 8 >>> l=[1,1,1]
 9 >>> d={'nice':111}
10 >>> print("before:","num=%f,l=%s,d=%s"%(num,l,d))
11 before: num=1.000000,l=[1, 1, 1],d={'nice': 111}
12 >>> fun(num,l,d)
13 inside: num=123.000000,l=[1, 2, 3],d={'a': 123}
14 >>> print("after:","num=%f,l=%s,d=%s"%(num,l,d))
15 after: num=1.000000,l=[1, 1, 1],d={'nice': 111
It should be noted: internal function num, l, d, and the command line of num, l, d are different variables, just the same name. It is a function of the internal parameter, the command line arguments.
Modify the contents of the object passed in, that is not to make formal parameters point to a different object, but an object by reference to modify the content. Of course, this object must be variable.
 1 >>> def fun2(num1,l1,d1):
 2 ...     num1=123
 3 ...     l1[0]=123
 4 ...     d1['a']=123
 5 ...     print("inside:","num1=%f,l1=%s,d1=%s"%(num1,l1,d1))
 6 ... 
 7 >>> num=111c
 8 >>> l=[1,1,1]
 9 >>> d={'a':111,'b':0}
10 >>> print("before:","num=%f,l=%s,d=%s"%(num,l,d))
11 before: num=111.000000,l=[1, 1, 1],d={'a': 111, 'b': 0}
12 >>> fun2(num,l,d)
13 inside: num1=123.000000,l1=[123, 1, 1],d1={'a': 123, 'b': 0}
14 >>> print("after:","num=%f,l=%s,d=%s"%(num,l,d))
15 after: num=111.000000,l=[123, 1, 1],d={'a': 123, 'b': 0}

 

Summary: python are in all object references to the function object is passed, when the parameter points to a different object, does not change the argument; when passing through a reference parameter to modify the content object, real participants follow changes as the parameter and the reference point to the same object. In which not only is the transfer function parameters is such that, inside this function also. When a variable assignment to another variable, is a reference to the object, the object reference, the address corresponding to the feeling c language variables, but in python, the address variable is versatile, can point to any type, when you change the address, which corresponds to a python pointing to different objects, the address of the original variable is not changed, so the contents of the original variable to point to the address has not changed. When you change the contents of the address inside pass this address, the contents of the original variable points to the address will also change.

 



 

Guess you like

Origin www.cnblogs.com/qq991025/p/11691370.html