Python pass by assignment

在C++里pass by value和pass by reference是兩個(gè)很容易區(qū)分的東西。但是在python中,讓我有點(diǎn)糊涂,其實(shí)Python是“pass by assignment”。


舉個(gè)例子:

>>> 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]這一個(gè)list object。這個(gè)list object的變化(append 4)使得a和b都發(fā)生了變化。但是,當(dāng)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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容