[Python-Dev] Let's stop eating exceptions in dict lookup

Scott Dial scott+python-dev at scottdial.com
Tue May 30 01:48:56 CEST 2006


Raymond Hettinger wrote:
> If it is really 0.5%, then we're fine.  Just remember that PyStone is an 
> amazingly uninformative and crappy benchmark.

Since Armin seems to not like having to justify his patch with any 
performance testing, I wrote a handful of dict insertion exercises and 
could find no real performance impact. In the case of an exception, it 
was much faster, but that is because it is not inserting anything into 
the dictionary.

IOW if it is a bad change in behavior. Previously, the exception was 
swallowed and it was assumed to be a new key despite the exception. This 
is an obscure use case that could creep up it's ugly head.

class K(int):
    def __cmp__(self, o): raise Exception()

d = {}
for i in xrange(10):
     d[K()] = i
for k in d.keys():
     print d[k]

Despite the incomparability, this throws no error in previous versions 
and the dict is still usable for the expected purpose.

-- 
Scott Dial
scott at scottdial.com
scodial at indiana.edu


More information about the Python-Dev mailing list