why is y not also print empty dictionary?
x = {}
y = x
print y
x['key'] = 'value'
print y
x = {}
print x,y
Result:
{}
{'key': 'value'}
{} {'key': 'value'}
why is y not also print empty dictionary?
x = {}
y = x
print y
x['key'] = 'value'
print y
x = {}
print x,y
Result:
{}
{'key': 'value'}
{} {'key': 'value'}
First you must understand that:
Whenever you say {} in your code, Python creates a new dictionary. It's basically a shorter syntax for dict().
Variables in Python are not really variables, they are names. You can think of them like labels that you attach to values, similar to price tags in stores. This means you can move them around as you wish and the objects themselves wont change.
Let's go through the meaningful lines in your code:
x = {}
y = x
x['key'] = 'value'
x = {}
Lines:
x = {}: We create a new dict using the {}, and then we set the name x to refer to that new dictionary.y = x: Here we set the name y to point to the existing dict, the one which already has name x tagged on it. The dict now has two names. Notice that y points to the actual dict, and it doesn't care whether the name x changes to point to something else. Similar to price tags in stores, you can move one without touching the otherx['key'] = 'value': Simply add an item to the dict that x points to. This could also be y['key'] = 'value' and the output would be the exact same, since x and y are attached to the same dict.x = {}: Notice the {} which means we're creating a new dictionary! We also set the name x to point to this new dictionary -- leaving the original dict and the name y untouched, so y still refers to the original dict with an item in it.And that's the end of your code, now printing x will output an empty dict (line 4), yet y still points to the original dict.
Assignments are not by value, but by reference. Not the value is copied from x to y, but the reference to the object currently held by x. So, after y = x, both variables actually reference the same object.
Edit:
x = {}
x references new dict object, call it dA
y = x
x, y both reference dA
x['key'] = 'value'
entry added to dA
x = {}
x references new dict object dB
print x,y
y still references dA, x references dB
You only change the references the the objects. And all references to an object are independent of each other, so if you change one, you do not alter the others.
You reassign x, but don't reassign y.
Initially they share the same reference, so changing the object they reference will reflect in both (as demonstrated when assigning the key/value). But if you change the reference of one of the variable, it won't change the reference of the other. Instead change the object referenced:
>>> x = {}
>>> y = x
>>> x['item'] = 'value'
>>> x
{'item': 'value'}
>>> y
{'item': 'value'}
>>> del x['item']
>>> x
{}
>>> y
{}
In other words. y = x does not mean that y is x, it means that y's referenced object is the same as x's referenced object.
y is a name. Variables are names, simple tags to identify a memory location.
When you assign y = x, y points to the same place x does (i.e. to the same dictionary). Python does not create a new copy of x: it simply makes y point to the same place x points to and keeps track of how many references the value (dictionary) has to it (i.e. the variables that refer to it).
Therefore, after you have added an item to the dictionary with x['key'] = 'value', y shows you the same value x does.
When you reassign a new value to x with x = {}, y does not change: it still points to the dictionary it pointed to before, so now you have two distinct dictionaries.
Clear enough? :)
When you do x = {} for the second time, you assign {} to x, but y still points to the old dictionary
Originally both x and y were referencing the same dictionary. Then, you created a new, empty dictionary and pointed x at it. Thus, y is still referencing the original dictionary, while x is not.
The reason is that when you are doing y = x x and y both refer to the same dictionary object.
Later when you are saying x={}, it creates a new, empty dictionary and assign it to x. This leaves y pointing to the same old dictionary. Therefore print x, y gave different value for x and y.
If you want to empty the dictionary, use x.clear(), this will empty out the dictionary, and still x and y will be pointing to the same old dictionary.
Refer: Difference between dict.clear() and assigning {} in Python