Why does the string representation of KeyError
add extra quotes to the error message? All other built-in exceptions just return the error message string directly.
For example, the following code:
print str(LookupError("foo"))
print str(KeyError("foo"))
Produces the following output:
foo
'foo'
I have tried this with a sampling of other built-in exceptions (IndexError
, RuntimeError
, Exception
, etc) and they all return the exception message without the quotes.
help(KeyError)
says that __str__(...)
is defined in KeyError
, as opposed to the LookupError
, which uses the one defined in the BaseException
base class. This explains how the behavior is different, but doesn't explain why __str__(...)
is overridden in KeyError
. The Python docs on built-in exceptions don't shed any light on this discrepancy.
Tested against Python 2.6.6