Directly on the code:
First = 1second = 2def haha (first=first,second=second): #默认值是上面定义的两个变量 print "first:%d"%first print "second:%d"%sec Ondhaha () First + = 5second + = 6haha ()
What about the output?
First of all, look at the results, you can imagine that there are two results:
One is haha the default value of this function is changed with both first and second variables, that is, 5 and 6 are obtained after calling haha in turn.
One is haha the default value of this function does not change with the first and second variables, that is, the output 1 and 2 are still behind.
You can run it yourself, the result is the second of the above, which is still output 1 and 2
That is, when the function is initialized, its default argument is to pass the value in instead of the variable's address (the pointer? Python does not, but can pass the ID of the function)
Are all the types like this?
I tried the following one, others please try it yourself:
List type variables do default parameters
L1 = [1,]def haha (l1=l1): Print L1haha () l1.append (2) haha () L1 = [3,] #注意重新赋值和上面的区别haha ()
Details can be found in the section: Default arguments in Python
Report:
For the default value of the first int type, if you want to change the partial function that can use the standard library functools,
For details see change default values of existing function ' s parameters?
Default parameters for Python functions