我做了以下功能:
def func(x): y = [4,5,6] x = ydef funcsecond(x): y = [4,6] x[1] = y[1]x = [1,2,3]
当我调用func(x)然后打印出x时,它打印出[1,3],就像x之前的那样,它不会将列表y分配给x.但是,如果我调用funcsecond(x),它会将5分配给x的第二个位置.为什么会这样?当我分配整个列表时,它不会做任何事情,但是当我只分配一个元素时,它会更改我最初调用它的列表.
非常感谢你,我希望你明白我打算说什么,我很难用英语表达自己.
def func(x): # here x is a local variable which refers to the object[1,3] y = [4,6] x = y #Now the local variable x refers to the object [4,6]def funcsecond(x): # here x is a local variable which refers to the object[1,6] x[1] = y[1] # it means [1,3][1]=5,means you changed the object x was pointing tox = [1,3]总结
以上是内存溢出为你收集整理的函数中的Python列表引用.全部内容,希望文章能够帮你解决函数中的Python列表引用.所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)