I have some python code that's throwing a KeyError exception. So far I haven't been able to reproduce outside of the operating environment, so I can't post a reduced test case here.
The code that's raising the exception is iterating through a loop like this:
for k in d.keys():
if condition:
del d[k]
The del[k] line throws the exception. I've added a try/except clause around it and have been able to determine that k in d is False, but k in d.keys() is True.
The keys of d are bound methods of old-style class instances.
The class implements __cmp__ and __hash__, so that's where I've been focusing my attention.
k in d.keys() will test equality iteratively for each key, while k in d uses __hash__, so your __hash__ may be broken (i.e. it returns different hashes for objects that compare equal).
Simple example of what's broken, for interest:
>>> count = 0
>>> class BrokenHash(object):
... def __hash__(self):
... global count
... count += 1
... return count
...
... def __eq__(self, other):
... return True
...
>>> foo = BrokenHash()
>>> bar = BrokenHash()
>>> foo is bar
False
>>> foo == bar
True
>>> baz = {bar:1}
>>> foo in baz
False
>>> foo in baz.keys()
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With