在C++里pass by value和pass by reference是两个很容易区分的东西。但是在python中,让我有点糊涂,其实Python是“pass by assignment”。
举个例子:
>>> a=[1,2,3]
>>> b=a
>>> b
[1, 2, 3]
>>> a.append(4)
>>> b
[1, 2, 3, 4]
>>> a=[1]
>>> b
[1, 2, 3, 4]
可以看到,a和b一开始都是bind to [1,2,3]这一个list object。这个list object的变化(append 4)使得a和b都发生了变化。但是,当a bind to another object [1], b is still pointing to the original object, and the original object doesn't change.
This property properly determines when calling a function, how the parameter is passed. Personally I would understand that as "pass by object".
Reference
stack overflow and quora
网友评论