16 | Pass by value, pass by reference or other, how are parameters passed in Python?

1. What is passing by value and passing by reference

Value transfer is usually to copy the value of the parameter and then transfer it to the new variable in the function, so that the original variable and the new variable are independent of each other and do not affect each other.

#include <iostream>
using namespace std;
 
// 交换 2 个变量的值
void swap(int x, int y) {
int temp;
temp = x; // 交换 x 和 y 的值
 x = y;
 y = temp;
 return;
}
int main () {
 int a = 1;
 int b = 2;
 cout << "Before swap, value of a :" << a << endl;
 cout << "Before swap, value of b :" << b << endl;
 swap(a, b); 
 cout << "After swap, value of a :" << a << endl;
 cout << "After swap, value of b :" << b << endl;
 return 0;
}
Before swap, value of a :1
Before swap, value of b :2
After swap, value of a :1
After swap, value of b :2

This swap() function copies the values ​​of a and b to x and y, and then exchanges the values ​​of x and y. In this way, the values ​​of x and y have changed, but a and b are not affected by them, so the values ​​remain unchanged. This method is what we call value transfer.

Passing by reference usually refers to passing the reference of the parameter to the new variable, so that the original variable and the new variable will point to the same memory address. If you change the value of any one of these variables, the other variable will also change accordingly.

void swap(int& x, int& y) {
   int temp;
   temp = x; // 交换 x 和 y 的值
   x = y;
   y = temp;
   return;
}

result:

Before swap, value of a :1
Before swap, value of b :2
After swap, value of a :2
After swap, value of b :1

The values ​​of the original variables a and b are swapped, because the reference passing makes a and x, b and y exactly the same, any changes to x and y will inevitably lead to the corresponding changes in a and b.

2.Python variables and their assignments

a = 1
b = a
a = a + 1

First assign 1 to a, that is, a points to this object, flow chart

Then b=a means that the variable b also points to the object 1 at the same time. Note here that the object in python can be pointed to or referenced by multiple variables.

Finally, execute a=a+1. It should be noted that the data types of python, such as integers, strings, etc., are immutable. So a = a+1, not to increase the value of a by 1, but to recreate a new object with a value of 2, and let it point to it. But b remains unchanged, still pointing to the object 1. So the result is:

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 other values.

l1 = [1, 2, 3]
l2 = l1
l1.append(4)
l1
[1, 2, 3, 4]
l2
[1, 2, 3, 4]

Similarly, we first make the lists l1 and l2 point to the object [1,2,3] at the same time.

Since the list is mutable, l1.append(4) will not create a new list, but insert element 4 at the end of the original list, which becomes [1,2,3,4]. Because l1 and l2 point to this at the same time List, so the change of the list will be reflected on the variables l1 and l2 at the same time, then the values ​​of l1 and l2 will become [1,2,3,4] at the same time

Variables in python can be deleted, but objects cannot be deleted.

del deletes this variable and can no longer be accessed, but the object [1,2,3] still exists. When the pyhton program is running, its built-in garbage collection system will track the references of each object. If [1,2,3] is referenced elsewhere except 1, it will not be recycled, otherwise it will be recycled.

such as:

def my_func1(b):
	b = 2

a = 1
my_func1(a)
a
1

Guess you like

Origin blog.csdn.net/yanyiting666/article/details/93386160