I understand that mutating dictionaries during a loop over them can cause runtime errors, but I know that there are the right ways to do this, and I cannot figure out which ones are acceptable and which are not. Console testing does not help them, because they MAY produce errors at runtime, I'm not sure if I get it right or just get luck.
Let's say I have an attribute, a status, which is a status dictionary:
self.status = {"overall": False, "started":False,
"ready":False, "awakeQueried":False,
"allQueried":False}
They change to various Trues or Falses during operation. I have a function that handles a failure and wants to set all of them to False. What an elegant way? While this works for me, but I'm not sure that at some point I will encounter a runtime error:
self.status = {key:False for key in self.status}
Can I do it? Here I am not sure about the low level functioning. I am not making a copy with .items () or the like. I am rewriting the dictionary, but first it has to process the right side, but this is understanding, so I'm not sure if it completely completes the full understanding before rewriting self.status. Does it keep some kind of copy in memory while it understands the new dict and then sets it up or does it cycle through and determine the elements after each iteration (where I think there will be problems)?
source
share