On Tue, 22 Dec 2009 17:47:14 +0100, Christian Heimes wrote: > John schrieb: >> Hi there, >> >> I have a rather lengthy program that troubles me for quite some time. >> After some debugging, I arrived at the following assertion error: >> >> for e in edges.keys(): >> assert edges.has_key(e) >> >> Oops!? Is there ANY way that something like this can possibly happen? > > Yes, it happens when another part of your program -- most likely a > thread -- modifies edges while you are iterating over its keys. The > keys() method of a dict returns a *copy* of its keys. If you had uses > "for e in edges" you'd have seen a RuntimeError "dictionary changed size > during iteration".
To be pedantic, you *might* have seen a RuntimeError, as the heuristic for detecting modifications during iteration is fairly simple and can only detect changes that change the size of the dict. >>> d = {1: 'a', 2: 'b', 3: 'c'} >>> n = 1 >>> for key in d: ... del d[n] ... d[str(n)] = None ... n += 1 ... >>> d {'1': None, '2': None, '3': None} -- Steven -- http://mail.python.org/mailman/listinfo/python-list