Does Python make a copy of objects on assignment?

Python

Python Problem Overview


I would expect that the following code would just initialise the dict_a, dict_b and dict_c dictionaries. But it seams to have a copy through effect:

dict_a = dict_b = dict_c = {}
dict_c['hello'] = 'goodbye'

print dict_a
print dict_b
print dict_c

As you can see the result is as follows:

{'hello': 'goodbye'}
{'hello': 'goodbye'}
{'hello': 'goodbye'}

Why does that program give the previous result, When I would expect it to return:

{}
{}
{'hello': 'goodbye'}

Python Solutions


Solution 1 - Python

This is because in Python, variables (names) are just references to individual objects. When you assign dict_a = dict_b, you are really copying a memory address (or pointer, if you will) from dict_b to dict_a. There is still one instance of that dictionary.

To get the desired behavior, use either the dict.copy method, or use copy.deepcopy if your dict may have nested dicts or other nested objects.

>>> a = {1:2}
>>> b = a.copy()
>>> b
{1: 2}
>>> b[3] = 4
>>> a
{1: 2}
>>> b
{1: 2, 3: 4}
>>> 

Solution 2 - Python

Even though

>>> dict_a, dict_b, dict_c = {}, {}, {}

is the right way to go in most cases, when it get more than 3 it looks weird

Imagine

>>> a, b, c, d, e, f = {}, {}, {}, {}, {}, {}

In cases where I wanna initialize more than 3 things, I use

>>> a, b, c, d, e, f, = [dict() for x in range(6)]

Solution 3 - Python

As danben previously said, you're just copying the same dict into 3 variables, so that each one reffers to the same object.

To get the behaviour you want, you should instanciate a different dict in each variable:

>>> dict_a, dict_b, dict_c = {}, {}, {}
>>> dict_c['hello'] = 'goodbye'
>>> print dict_a
{}
>>> print dict_b
{}
>>> print dict_c
{'hello': 'goodbye'}
>>>

Solution 4 - Python

Your first assignment assigns the same dictionary object to the variables dict_a, dict_b, and dict_c. It is equivalent to dict_c = {}; dict_b = dict_c; dict_a = dict_c.

Solution 5 - Python

I agree with what is said above. The key here is that, in Python, assignments represent references to the object. I was trying to grasp the concept myself and I think is it important to understand in which case a new object is created and when is the existing one changed.

In the example above, the line:

dict_c['hello'] = 'goodbye'

doesn't create a new object. It only changes the object which is referenced by dict_a, dict_b, and dict_c.

If, instead, you wrote:

dict_c = {'hello': 'goodbye'}

it would create a new object which would be referenced by dict_c. Dict_a and dict_b would still be pointing to the empty object.

In that case, if you run:

print dict_a
print dict_b
print dict_c

you would get:

{}
{}
{'hello': 'goodbye'}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcus WhybrowView Question on Stackoverflow
Solution 1 - PythondanbenView Answer on Stackoverflow
Solution 2 - PythonJeffrey JoseView Answer on Stackoverflow
Solution 3 - PythonmdeousView Answer on Stackoverflow
Solution 4 - PythonmsalibView Answer on Stackoverflow
Solution 5 - PythonDomagoj VidovićView Answer on Stackoverflow