Pythonは関数に指定した引数がすべて参照渡しとなるらしい。
ただし、指定した引数の型によっては変更不可となる。
変更不可(イミュータブル)
int, float, str, tuple
変更可(ミュータブル)
list, set, dict
↓参考
http://docs.python.jp/2/reference/datamodel.html
■変更不可例
#-*- coding:utf-8 -*-
def test(gg):
gg = gg * 2
if __name__ == "__main__":
x = 10
print(x) ## 結果 10
test(x)
print(x) ## 結果 10
■変更可例
#-*- coding:utf-8 -*-
def test(list):
list[0] = list[0] * 2
if __name__ == "__main__":
list = []
list.append(10)
print list[0] ## 結果 10
test(list)
print list[0] ## 結果 20
id関数を使ってみると、関数内でイミュータブル変数を変更すると識別子が変わった。
(ミュータブルは識別子変わらず)
#-*- coding:utf-8 -*-
def test(list,x):
print id(list),id(x) # 7696579635896 25769969816
list[0] = list[0] * 2
x += 1
print id(list),id(x) # 7696579635896 25769969792
if __name__ == "__main__":
x = 1
list = []
list.append(10)
print list[0],x # 10 1
print id(list),id(x) # 7696579635896 25769969816
test(list,x)
print list[0],x # 20 1
print id(list),id(x) # 7696579635896 25769969816
