Before analyzing how the parameter transfer in python is carried out, we need to understand the basic principles of python variables and assignment, which will help us better understand parameter transfer.
Python variables and assignment
Start with a few lines of code
In [1]: a =1
In [2]: b = a
In [3]: a = a +1
We first assign 1 to a, that is, a points to the object 1, and everything is an object in python. Then b=a, which means that b also points to the object 1. In python, an object can be pointed to by multiple references. The last executed a=a+1, we need to pay attention to one point here. In python's data types such as int, str and other immutable types, performing a=a+1 does not increase the value of the object pointed to by a by 1. Instead, create a new object 2, and let a point to the object 2. The original object still exists in memory. Then the words here will still be pointed, let's take a look at the values of a and b respectively:
In [4]: a
Out[4]:2
In [5]: b
Out[5]:1
Through this example, you can see that here a and b are just two variables pointing to the same object at the beginning, or you can also imagine them as two names of the same object. The simple assignment of b = a does not mean that a new object is re-created, but that the same object is pointed to or referenced by multiple variables. At the same time, pointing to the same object does not mean that the two variables are bound together. If you reassign one of the variables, it will not affect the value of the other variables.
There is also an example of a list, let's take a look again:
In [6]: l1 =[3,4,5,6]
In [7]: l2 = l1
In [10]: l1.append(7)
In [11]: l1
Out[11]:[3,4,5,6,7]
In [12]: l2
Out[12]:[3,4,5,6,7]
In the code, we let the two variables l1 and l2 point to the object [3,4,5,6]. We know that the list is a variable data structure, so the append operation does not generate new objects. It just adds an element at the end and becomes [3, 4, 5, 6, 7]. Since l1 and l2 point to this list at the same time, the change of the list will be reflected on both variables l1 and l2 at the same time, then , The values of l1 and l2 become [3, 4, 5, 6, 7] at the same time.
Variables in python can be deleted, but objects cannot be deleted
In [22]: a =[1,4,5]
In [23]: del a
When the del statement deletes the variable a, you cannot access [1,4,5] through a, but this object still exists in existence, and Python's garbage collection mechanism finds that the reference is 0, it will be recycled.
Variables can be deleted, but objects cannot be deleted
How does python function pass parameters
Python's parameter transfer is assignment transfer or reference transfer. Everything in python is an object, so when passing parameters, just let the new variable and the original variable point to the same object. Let's look at an example:
In [28]: def func(b):...: b =2
In [29]: a =1
In [30]:func(a)
In [31]: a
Out[31]:1
The parameter passing here makes the variables a and b point to the object 1 at the same time. But when we execute to b = 2, the system will recreate a new object with a value of 2, and let b point to it; and a still points to the object 1. Therefore, the value of a remains unchanged and remains 1.
How to change the value of a?
We can return b in the function
def func(b):
b =2return b
a =1
a =func(a)
a
2
In the above example, we are of type int, let's take a look at the list example:
def func(l2):
l2.append(77)
l1 =[12,3,6]func(l1)
l1
[12,3,6,77]
Here l1 and l2 first point to lists with values [1, 2, 3] at the same time. However, because the list is variable, when the append() function is executed and a new element 4 is added to the end, the values of the variables l1 and l2 also change.
Then everyone look at the following example, what is the result?
def func(l2):
l2 = l2 +[4]
l1 =[12,3,6]func(l1)
l1
[12,3,6]
As you can see, l1 has not changed, because the operation of l2 + [4] means that a new list is created with element 4 added at the end, and l2 points to this new object, and l1 points to the original object.
to sum up
Today, we discussed the basic principles of Python variables and their assignments, and explained how parameters are passed in Python. Unlike other languages, parameter passing in Python is neither by value nor by reference, but by assignment, or called object by reference. It should be noted that the assignment or object reference transfer here does not point to a specific memory address, but to a specific object.
The above is the detailed content of python's parameter passing method. For more information on how python performs parameter passing, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts