[issue18163] Add a 'key' attribute to KeyError

Mark Dickinson report at bugs.python.org
Thu Jun 13 17:47:53 CEST 2013


Mark Dickinson added the comment:

+1.  I recently chastised a colleague for doing "raise KeyError(long_message)" instead of "raise KeyError(missing_item)".  When I went to the standard library to support my POV, I found (to my chagrin) a big mix of the two styles.


>>> from collections import ChainMap
>>> d = ChainMap({}, {})
>>> try:
...     d.pop('not there')
... except KeyError as e:
...     key, = e.args
... 
>>> key
"Key not found in the first mapping: 'not there'"

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18163>
_______________________________________


More information about the Python-bugs-list mailing list