#引子 First Look at an instance:
def change (val):
val.append (m)
val = [' T ', ' Z ', ' Y ']
nums = [0, 1] change
(nums)
print (nums)
Guess what the result should be. According to the C + + language, if the python function parameter is passed a value, the result should be [0, 1], and if it is a reference, the result should be [' T ', ' Z ', ' Y ']. But the actual result is: [0, 1, 100]. Therefore, the transfer of Python function parameters is neither a so-called pass value nor a reference. #Python函数参数传递 So what is the function parameter in Python passed in the end? Let's begin by defining some concepts: the * * variable in Python and the * * Object * *. There is no type of variable in Python, we can think of it as a pointer to a (*void) type, a variable can point to any object, and the object is a type. and objects in Python have immutable objects (number,string,tuple, etc.) and variable objects (LIST,DICT, etc.). For example, the following examples:
Nums = (1, 2, 3)
type (nums) #输出: tuple
ID (nums) #输出: 59179256
nums = [1, 2, 3]
type (nums) #输出: List
ID (n UMS) #输出: 59094960
You can see that nums is not a type, it can point to a tuple or point to a list, and you can see from the ID that nums points to different objects. Having understood this concept, we can say that the passing of a function parameter in **python is the value of the passed variable, that is, the address of the object to which the variable refers. In general, we have the following rules: 1. Immutable objects, as function parameters, correspond to the value transfer of the C-system language. 2. The variable object acts as a function parameter, which corresponds to the reference delivery of the C-system language. But, in fact, as long as our hearts remember: The parameters are passed by the variable to the object of the address of the line, value delivery and reference delivery are the concepts in C + +. #例子分析 below we analyze a few examples: 1. Immutable objects
def change (val):
val = 0
num = 1 change
(num)
print (num) #输出结果为1
According to the above rule 1, the output result is 1. Let's analyze why.
We have defined a variable num,num pointing to the number 1, then, when the change function is executed, the NUM variable is copied to Val, which is just entering the function body when Val still points to number 1, and then the function body gives Val 2, because the number is immutable, so Val points back to 0. But as a num variable, it still points to 1.
2. Variable objects
def change (val):
val.append (1)
nums = [0] Change
(nums)
print (nums)
Here's the same thing with the example, when I first entered the change function, Val pointed to the list [0] because the list was a mutable object, so when performing a append operation on [0], the direct effect on the original list does not generate a new object, so the return result is [0, 1].
3. Examples in the intro
At the beginning, Nums points to the list [0], and then enters the function body change, Val points to list [0], then executes append, the list [0] becomes [0, 1], then assigns [' T ', ' Z ', ' Y '] to Val, so the final result is nums pointing to [ 0] became [0, 1].
So the most important thing is to understand that the passing of function arguments in Python is the address of the object to which the variable points, and the difference between the variables and objects in Python.