def myfunc(x):
y = x
y.append('How do I stop Python from modifying x here?')
return y
x = []
z = myfunc(x)
print(x)
views:
129answers:
2It's worth noting that the copy is shallow.
FM
2010-07-04 11:52:25
+1
A:
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)
Petriborg
2010-07-04 03:54:04