我的老师告诉我purchase_list=my_list
通过引用/地址传递。例如:
my_list = ['pen' , 'pencil' , 'cell phone' , 'axiom team' , 'panacloud' ]
purchase_list = my_list
my_list.append('x')
print(my_list)
print(purchase_list)
给出以下输出
['pen', 'pencil', 'cell phone', 'axiom team', 'panacloud', 'x']
['pen', 'pencil', 'cell phone', 'axiom team', 'panacloud', 'x']
那为什么当我执行以下操作时
:my_list = ['pen' , 'pencil' , 'cell phone' , 'axiom team' , 'panacloud' ]
purchase_list = my_list
my_list=["Naufil"]
print(my_list)
print(purchase_list)
给我输出:
['Naufil']
['pen', 'pencil', 'cell phone', 'axiom team', 'panacloud']
也不会
['Naufil']
['Naufil']