I have a custom made dictionary. It reloads the whole dictionary each time a field is accessed. For instance, I could have a loop like this:
my_dict=myDict('file.yml')
while(True):
print(my_dict['field'])
And this will change the output if I modify the file file.conf
while running. It is done by inheriting from collections.UserDict
and modifying __getitem__
as described in this question. In short, it looks like this:
class myDict(collections.UserDict):
def __init__(self, filename):
super().__init__()
self.config_filename=filename
self.reload()
def __getitem__(self, key):
self.reload()
return super().__getitem__(key)
def reload(self):
with open(self.config_filename) as f:
data=yaml.safe_load(f)
super().__init__(data)
I tried this code:
my_dict=myDict('file.yml')
for e in my_dict['test']:
print(e)
time.sleep(5)
While running, I modified the yaml file, but it did not have any effect, which is what I want. But I do wonder how much I can trust this. My assumption here is that my_dict['test']
will only be accessed once in this for loop, which means that my_dict.__getitem__
will only be called once. Is this correct?
I also tried modifying the values within the loop like this:
for e in my_dict['test']:
print(e)
print(my_dict['test'][e])
my_dict['test']['me']='hehehehe'
time.sleep(5)
At the same time I also modified the file, but none of the changes showed in the printouts. So what's going on here? From my experiments, it seems like the for loop is equivalent to something like:
my_copy = copy.deepcopy(my_dict):
for e in my_copy:
But it feels risky to assume that this is safe.