python与c++在变量间赋值时的行为不一样。
c++中对变量赋予新值时,变量指向的地址不变。
python中对变量赋予新值时,变量指向新值的地址。(python中值本身有自己的地址)
见代码:
c++ code:
#include <iostream>
using namespace std;
int main() {
int a = 1; //数据(1)储存在a所在的内存空间
int b = a; //值传递,此时有两份数据(1)
cout << "a: "<< a <<endl;
cout << "b: "<< b <<endl;
cout << "a add: "<< &a <<endl;
cout << "b add: "<< &b <<endl<<endl;
a = 2; //对a所在的空间赋予新的数据(2),对b没有任何影响
cout << "a: "<< a <<endl;
cout << "b: "<< b <<endl;
cout << "a add: "<< &a <<endl;
cout << "b add: "<< &b <<endl<<endl;
return 0;
}
</