[issue18163] Add a 'key' attribute to KeyError

Brett Cannon report at bugs.python.org
Wed Jun 12 19:36:23 CEST 2013


Brett Cannon added the comment:

What David said. =)

The fact that the key is the first value from args is almost happenstance as the exception message is customized in __str__() and not in what is passed to the exception. But what if I had defined __getattr__ or __getattribute__ and had some prefix requirement? It would be more helpful to say ``KeyError("{!r} does not start with py_".format(key))`` which breaks your args[0] usage but is a much more descriptive message and has no defined way to provide the key as an attribute.

It's basically "explicit is better than implicit" since there is no API guarantee that the first thing in 'args' for KeyError will in actuality be the key.

----------

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


More information about the Python-bugs-list mailing list