When using tuples of length one as keys for the builtin
dictionary the Python runtime raises an inaccurate
KeyError exception that makes some errors hard to find:
Python 2.5 (r25:51908, Dec 12 2006, 18:39:30)
[GCC 4.1.1 (Gentoo 4.1.1-r1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> {"foo":"bar"}[("foo",)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'foo'
Also the error messages for the empty tuple and None are
indistinguishable:
Python 2.5 (r25:51908, Dec 12 2006, 18:39:30)
[GCC 4.1.1 (Gentoo 4.1.1-r1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> {None:"bar"}[()]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError
>>> {():"bar"}[None]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError
This also seems to be the case for earlier versions of Python.
|