Re: [Python-Dev] Should KeyError use repr() on its argument?
The KeyError exception doesn't apply repr() to its argument. That's annoying in cases like this:
a = {} a[''] Traceback (most recent call last): File "<stdin>", line 1, in ? KeyError
Should this be fixed? How? (I guess we could add a KeyError__str__ method to exceptions.c that applies repr().)
I've got a feeling this is a feature, but not a very useful one.
I take it back. args[0] being the actual key that failed is a feature. str() not using repr() on args[0] is a bug. I'll fix it.
What is args[0]?
args is the name of the instance variable that most exceptions use to store the arguments that were passed to them in the raise statement (or equivalent C API). It is a tuple. Examples:
a = KeyError() a.args () a = KeyError(1) a.args (1,) a = KeyError(1,2,3) a.args (1, 2, 3) try: {}[''] except KeyError, k: print k.args
('',)
Are you saying that dicts use repr() instead of str() to get the key value when accessing?
No, I'm saying that str(KeyError('foo')) should return repr('foo') rather than 'foo' as it does now. See current CVS. :-) --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (1)
-
Guido van Rossum