开发者

difficulty with Python

def myfunc(x):
 y = x
开发者_Go百科 y.append('How do I stop Python from modifying x here?')
 return y

x = []
z = myfunc(x)
print(x)


You do:

y = x[:]

to make a copy of list x.


You need to copy X before you modify it,

def myfunc(x):
 y = list(x)
 y.append('How do I stop Python from modifying x here?')
 return y

x = []
z = myfunc(x)
print(x)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜