I'm expecting my frustration to be overridden with some enlightenment - here's a minimal version of the script to demonstrate the problem:
First I create a dictionary:
dic = {
'foo':{},
'bar':{}
}
Then we instantiate a template dictionary that can be iteratively appended
to keys of dic
:
appendic= {
'is':'', # '' is a terminal value to be replaced later
}
So here we append appendic
to each of the keys in dic
:
dic['foo'] = appendic
dic['bar'] = appendic
Now we replace the terminal values, '', with something meaningful:
dic['foo']['is'] = 'foo'
dic['bar']['is'] = 'bar'
At this point, my intuition tells me that if we call:
print(dic['foo']['is'])
we get 'foo'
But instead Python returns 'bar'
... to my un-trained mind that is counter-intuitive.
Questions:
- How can I tell Python to keep the keys of dic independent?
- Why is this the default behaviour? What use cases does this have?