Have you ever written some code and had it not behave unexpectedly? Maybe an object value was incorrect and the error was difficult to trace back?
The reason for an unexpected value like this could be simpler than you think.
When you attempt to create a copy of an object in Python, by default it only assigns a new name to the existing object.
Let's break that down. Say you have a list called my list
. It looks like this:
my_list = [1, 2, 3, 4, 5, 6, 7]
Now you want to create a copy of your list, so you do the following:
my_second_list = my_list
Now when you execute print(my_list, my_second_list)
, you get:
[1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7]
So far, so good.
Now, say you want to change my_list
by adding 8
. You can do this with:
my_list.append(8)
Now you can make sure that 8
has been added to my_list
by executing print(my_list)
, and you get:
[1, 2, 3, 4, 5, 6, 7, 8]
So far, so good. But what about my_second_list
? Let's check to make sure it still contains the integers 1 through 7 by executing print(my_second_list)
. You get:
[1, 2, 3, 4, 5, 6, 7, 8]
Uh oh... that's NOT the behaviour we're looking for. This occurs because of the naming behaviour I mentioned above.
What if you want to make sure you have two SEPARATE copies of your object?
This can be accomplished using a deepcopy operation. deepcopy
works as follows:
from copy import deepcopy
my_list = [1, 2, 3, 4, 5, 6, 7]
my_second_list = deepcopy(my_list)
Now if we append 8
to my_list
, we get the expected behaviour:
my_list.append(8)
print(my_list, my_second_list)
> [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7]
Voilà! my_list
and my_second_list
now function independently of each other.