Let's talk about mutable objects and immutable objects in Python before you talk about function parameters.
I. mutable objects and immutable objects
In Python, everything is object, there is no so-called call-to-value in Python, and all that is passed is a reference to the object, or it can be thought of as a pass-through. The so-called mutable object is that the content of the object is variable, and the immutable object refers to the object content is not mutable (that is, after it is created, the value cannot be changed, but the new object can be created and assigned with the same variable name, and the old object is cleaned out, which is called garbage collection of the object in Python).
Immutable (immutable): int, string, float, (numeric number), tuple (tuple)
Variable (mutable): Word Typical (dictionary), list type (lists)
Look at the following example:
i = 73i + = 2
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/86/58/wKiom1e8TmGx9YCcAAFIwBCJa5E086.png-wh_500x0-wm_3 -wmp_4-s_4042868856.png "title=" Selection _001.png "alt=" Wkiom1e8tmgx9yccaafiwbcja5e086.png-wh_50 "/>
From the know, the invariant object's characteristics have not changed, but only created a new object, changed the object reference of the variable.
A = 1b = 1print A is b
The result of the output is true, which means that A and B point to the same object, which is called a shared reference in Python. In Python, the variable always points to the pointer to the object, not the label of the area of memory that can be changed: assigning a new value to a variable, not replacing the original object (the original object is still there, just not having the variable to refer to it, and finally being recycled by the garbage collection mechanism), but instead letting the variable refer to a completely different object
Variable Object Examples:
m = [5,9]m + = [6]
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/86/58/wKiom1e8UciBo7bsAAHNtDGuXGU388.png-wh_500x0-wm_3 -wmp_4-s_313920117.png "title=" Selection _003.png "alt=" Wkiom1e8ucibo7bsaahntdguxgu388.png-wh_50 "/>
The content of its objects can vary. When the content of an object changes, the object reference (that is, the memory address) of the variable does not change.
Second, the parameters of the function
Note For function passing parameters:
(1) parameter passing is achieved by automatically assigning an object to a local variable. The function parameter is just another instance of Python assignment in the actual application. Because the application is implemented as a pointer, all parameters are actually passed through the pointer.
(2) The parameter name assignment inside the function does not affect the caller.
(3) Changing the value of a variable object parameter of a function may have an effect on the caller
The third article should be carefully understood, as the following example
Def changer (A, b): a = 2 b[0] = "spam" x = 1L = [1,2]changer (x,l) Print x,l
The result of the output is not 1,[1,2] but 1,[' spam ', 2]
If you know the scope of Python, you will feel that x,l is a global variable, a, B is a local variable, after the call has no effect on the global variable ah, but note: L is a list, it is a mutable object, its value is given to B, the function of the second assignment is in situ to change the object, to B[0] The result of the assignment affects the value of L after the function returns. Instead of modifying B, it modifies part of the object currently referenced by B, and this change affects the caller.
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/86/58/wKiom1e8VtPCtFF-AAIrjJCFiH0226.png-wh_500x0-wm_3 -wmp_4-s_3025160315.png "title=" Selection _004.png "alt=" Wkiom1e8vtpctff-aairjjcfih0226.png-wh_50 "/>
Can be seen
. Immutable parameters are passed by value. Immutable objects such as integers are passed through object references rather than copies, but because no matter how immutable objects can be modified in the same place, the actual effect is much like creating a copy, a shallow copy
. mutable objects are passed through the pointer.
Therefore, in order to avoid changes in variable object parameters, you can use a deep copy to avoid this problem, that is to say, a deep copy of the variable object into the function, and then let the function call copy of the object. As follows:
Method One:
Def changer (A, b): a = 2 b[0] = "spam" x = 1L = [1,2]changer (x,l[:]) Print x,l
Method Two:
Def changer (A, B): b[:] A = 2 b[0] = "spam" x = 1L = [1,2]changer (x,l) Print x,l
If you do not know the deep copy and shallow copy, Google for yourself, do not introduce, hope understanding!
Three, parameter matching model
The matching model mainly has the following several:
(1) Position: match from left to right
(2) keyword parameter: match by parameter name
(3) Default parameter: Define parameter values for parameters that do not have an incoming value
(4) Variable parameters: Collect any number of parameters based on location or keywords
(5) variable parameter: Pass any number of parameters based on position or keyword
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/86/57/wKioL1e8XIewvDt3AANPTwyZofI287.png-wh_500x0-wm_3 -wmp_4-s_2104908775.png "title=" Selection _005.png "alt=" Wkiol1e8xiewvdt3aanptwyzofi287.png-wh_50 "/>
function parameters in Python