對于剛接觸Python不久的新手,Python的函數傳參機制往往會讓人迷惑。學過C的同學都知道函數參數可以傳值或者傳地址。比如下面這段代碼
點擊(此處)折疊或打開
void func(int input) {
input = 100;
}
int a = 0;
func(a);
printf("%d", a);
結果應該是打印0,a的值自始至終都沒有改變,因為傳遞給函數func的是a變量的一個拷貝。而下面這段代碼,
點擊(此處)折疊或打開
void func(int* input) {
*input = 100;
}
int a = 0;
func(&a);
printf("%d", a);
則會打印100,因為傳遞給func的參數是變量a的地址。
那么Python遇到類似情況是怎么樣處理的呢?以下摘自Mark Lutz的Learning Python第五版,
Python’s pass-by-assignment scheme isn’t quite the same as C++’s reference parameters
option, but it turns out to be very similar to the argument-passing model of the C
language (and others) in practice:
Immutable arguments are effectively passed “by value.”Objects such as integers
and strings are passed by object reference instead of by copying, but because
you can’t change immutable objects in place anyhow, the effect is much like making
a copy.
Mutable arguments are effectively passed “by pointer.”Objects such as lists
and dictionaries are also passed by object reference, which is similar to the way C
passes arrays as pointers—mutable objects can be changed in place in the function,
much like C arrays.
Of course, if you’ve never used C, Python’s argument-passing mode will seem simpler
still—it involves just the assignment of objects to names, and it works the same whether
the objects are mutable or not.
也就是說在Python中,不可變參數(Immutable arguments)都是可理解為傳值的,而可變參數(Mutable arguments)都是可以理解為傳地址的。而哪些類型是不可變參數呢,根據Mark的描述,numbers,strings,tuples都屬于不可變,而list,dict都屬于可變類型。看下面這段小程序,
點擊(此處)折疊或打開
def func(var1):
var1 = 10
var2 = 200
func(var2)
print var2
func的傳入參數是整數,值為200,在func內部,它試圖把傳入參數賦值為10,結果打印出來的值仍然是200,原因是func的參數是不可變類型。
再看下面這段程序,
點擊(此處)折疊或打開
def func(var1):
var1[0] = 100
var2 = [3,4,5,6,7]
func(var2)
print var2[0]
func的傳入參數是list,屬于可變類型,而函數試圖把這個list的第一個元素用100進行賦值,結果是var2的第一元素從3變為了100,最終打印出來的結果是100。
在實際工作中,如何保證參數在傳遞過程中不發生改變呢?一個辦法是傳遞參數的一份拷貝,比如需要傳一個列表L = [1,2,3]給函數func,那你可以寫成func(L[:])。另一個辦法是使用函數tuple,把list轉換為tuple,像這樣func(tuple(L))。