python学习笔记

#传参列表副本(不改变列表本身)
lst = [1, 5, 33, 58]

def func(a):
    a[0] = 99
    print(a)

print(lst)              #[1, 5, 33, 58]
func(lst[:])            #不改变lst, [99, 5, 33, 58]
func(lst.copy())     #不改变lst, [99, 5, 33, 58]
print(lst)               #[1, 5, 33, 58]

func(lst)   #改变lst, [99, 5, 33, 58]
print(lst)  #[99, 5, 33, 58]

打印结果:
[1, 5, 33, 58]
[99, 5, 33, 58]
[99, 5, 33, 58]
[1, 5, 33, 58]
[99, 5, 33, 58]
[99, 5, 33, 58]
原文地址:https://www.cnblogs.com/DuanLaoYe/p/6736474.html