Why KeyError string representation add extra quotation marks to the error message? All other built-in exceptions simply return an error message string directly.
For example, the following code:
print str(LookupError("foo")) print str(KeyError("foo"))
It produces the following output:
foo 'foo'
I tried this with a selection of other built-in exceptions ( IndexError , RuntimeError , Exception , etc.) and they all return an exception message without quotes.
help(KeyError) says that __str__(...) is defined in KeyError , unlike LookupError , which uses the one defined in the BaseException base class. This explains how the behavior is different, but does not explain why __str__(...) overridden in KeyError . Python docs on built-in exceptions do not shed light on this inconsistency.
Tested in Python 2.6.6
python exception keyerror
pzed
source share