The code that I am working on is changing my temporary variables, and I can’t figure out how to stop it. Essentially, this is the problem:
def example(array):
temp = array
for i in range whatever:
#change array
I need to change array, but keep temp the same.
The line
does not copy the contents of
array– it simply binds another name to the same object. How to actually copy an object depends on the type of the object. For a NumPy array, you can doFor a Python list, you can use the above line starting from Python 3.3; in eariler version, you can use
There are also the generic copying functions
copy()anddeepcopy()in the modulecopy.