2024-04-18 06:31:57 发布
网友
我知道我们在Python中传递参数时传递对象的引用。你知道吗
所以
def changer(b): b[0] = "spam" l = [1,2] changer(l) # l is now ["spam",2]
但是,如果我这么做了
changer(l[:]) # l remains [1,2]
在第二种情况下,当我传递list slice时,传递给函数的是什么?你知道吗
l[:]创建一个副本。请参见切片。副本被传递到函数中,函数修改l的副本。因此,l将保持不变。你知道吗
l[:]
All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list.
l[:]
创建一个副本。请参见切片。副本被传递到函数中,函数修改l的副本。因此,l将保持不变。你知道吗相关问题 更多 >
编程相关推荐