1、原始函数
def a(x,y):
while x:
temp = x.pop()
print(f"printing model:{temp}")
y.append(temp)
def b(x):
print("\nThe following model had been printed!")
for i in x:
print(i)
list1 = ["aaa","bbb","ccc"]
list2 = []
a(list1,list2)
b(list2)
printing model:ccc
printing model:bbb
printing model:aaa
The following model had been printed!
ccc
bbb
aaa
>>> list1 ## 原始列表变为空列表
[]
>>> list2
['ccc', 'bbb', 'aaa']
2、使用函数副本(列表分片复制)
def a(x,y):
while x:
temp = x.pop()
print(f"printing model:{temp}")
y.append(temp)
def b(x):
print("\nThe following model had been printed!")
for i in x:
print(i)
list1 = ["aaa","bbb","ccc"]
list2 = []
a(list1[:],list2)
b(list2)
printing model:ccc
printing model:bbb
printing model:aaa
The following model had been printed!
ccc
bbb
aaa
>>> list1 ## 原始列表没有改变
['aaa', 'bbb', 'ccc']
>>> list2
['ccc', 'bbb', 'aaa']