Specifically, I want to create a backup of a list, then make some changes to that list, append all the changes to a third list, but then reset the first list with the backup before making further changes, etc, until I’m finished making changes and want to copy back all the content in the third list to the first one. Unfortunately, it seems that whenever I make changes to the first list in another function, the backup gets changed also. Using original = backup didn’t work too well; nor did using
def setEqual(restore, backup):
restore = []
for number in backup:
restore.append(number)
solve my problem; even though I successfully restored the list from the backup, the backup nevertheless changed whenever I changed the original list.
How would I go about solving this problem?
The first thing to understand is why that
setEqualmethod can’t work: you need to know how identifiers work. (Reading that link should be very helpful.) For a quick rundown with probably too much terminology: in your function, the parameterrestoreis bound to an object, and you are merely re-binding that identifier with the=operator. Here are some examples of binding the identifierrestoreto things.So, in your function, when you say:
You are actually binding restore to a new list object you’re creating. Because Python has function-local scoping,
restorein your example is binding the function-local identifierrestoreto the new list. This will not change anything you’re passing in tosetEqualas restore. For example,Simplifying a bit, you can only bind identifiers in the currently executing scope — you can never write a function like
def set_foo_to_bar(foo, bar)that affects the scope outside of that function. As @Ignacio says, you can use something like a copy function to rebind the identifier in the current scope: