
2016-04-15 23:16 GMT+02:00 Ethan Furman <ethan@stoneleaf.us>:
It's an useful property. For example, let's say that you have a guard on globals()['value']. The guard is created with value=3. An unit test replaces the value with 50, but then restore the value to its previous value (3). Later, the guard is checked to decide if an optimization can be used.
I don't understand -- shouldn't the version be incremented with the value was replaced with 50, and again when re-replaced with 3?
Oh wait, I'm tired and you are right. Not increasing the value only helps on this code: dict[key] = value dict[key] = value # version doesn't change
If the dictionary values are modified during the loop, the dict version is increased. But it's allowed to modify values when you iterate on *keys*.
I don't understand. Could you provide a small example?
For example, this loop is fine: for key in dict: dict[key] = None In this loop, the dict version is increased at each loop iteration. For iter(dict), the check prevents a crash. The following example raises a RuntimeError("dictionary changed size during iteration"): d={1:2} for k in d: d[k+1] = None Victor