python函数参数传递机制_Python 学习笔记(一) 理解Python的函数传参机制
對于剛接觸Python不久的新手,Python的函數(shù)傳參機(jī)制往往會讓人迷惑。學(xué)過C的同學(xué)都知道函數(shù)參數(shù)可以傳值或者傳地址。比如下面這段代碼
點(diǎn)擊(此處)折疊或打開
void func(int input) {
input = 100;
}
int a = 0;
func(a);
printf("%d", a);
結(jié)果應(yīng)該是打印0,a的值自始至終都沒有改變,因?yàn)閭鬟f給函數(shù)func的是a變量的一個拷貝。而下面這段代碼,
點(diǎn)擊(此處)折疊或打開
void func(int* input) {
*input = 100;
}
int a = 0;
func(&a);
printf("%d", a);
則會打印100,因?yàn)閭鬟f給func的參數(shù)是變量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.”O(jiān)bjects 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.”O(jiān)bjects 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中,不可變參數(shù)(Immutable arguments)都是可理解為傳值的,而可變參數(shù)(Mutable arguments)都是可以理解為傳地址的。而哪些類型是不可變參數(shù)呢,根據(jù)Mark的描述,numbers,strings,tuples都屬于不可變,而list,dict都屬于可變類型。看下面這段小程序,
點(diǎn)擊(此處)折疊或打開
def func(var1):
var1 = 10
var2 = 200
func(var2)
print var2
func的傳入?yún)?shù)是整數(shù),值為200,在func內(nèi)部,它試圖把傳入?yún)?shù)賦值為10,結(jié)果打印出來的值仍然是200,原因是func的參數(shù)是不可變類型。
再看下面這段程序,
點(diǎn)擊(此處)折疊或打開
def func(var1):
var1[0] = 100
var2 = [3,4,5,6,7]
func(var2)
print var2[0]
func的傳入?yún)?shù)是list,屬于可變類型,而函數(shù)試圖把這個list的第一個元素用100進(jìn)行賦值,結(jié)果是var2的第一元素從3變?yōu)榱?00,最終打印出來的結(jié)果是100。
在實(shí)際工作中,如何保證參數(shù)在傳遞過程中不發(fā)生改變呢?一個辦法是傳遞參數(shù)的一份拷貝,比如需要傳一個列表L = [1,2,3]給函數(shù)func,那你可以寫成func(L[:])。另一個辦法是使用函數(shù)tuple,把list轉(zhuǎn)換為tuple,像這樣func(tuple(L))。
總結(jié)
以上是生活随笔為你收集整理的python函数参数传递机制_Python 学习笔记(一) 理解Python的函数传参机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外汇是什么意思通俗一点
- 下一篇: 房贷下款前还查征信不