[Python-ideas] Generator unpacking

Émanuel Barry vgr255 at live.ca
Mon Feb 15 19:49:33 EST 2016


> From: Steven D'Aprano
> Sent: Monday, February 15, 2016 7:35 PM
> Subject: Re: [Python-ideas] Generator unpacking
> 
> Showing the missing key in the error message goes all the way back to
> Python 1.5:
> 
> [steve at ando ~]$ python1.5 -c '{}["spam"]'
> Traceback (innermost last):
>   File "<string>", line 1, in ?
> KeyError: spam
> 
> Admittedly, the exception object itself doesn't keep a reference to the
> missing key, so you can't programmatically query it for the key [...]

Well, there's no `key` attribute for example, but the KeyError exception has
exactly one argument, the missing key.

>>> try:
...   {}[42]
... except KeyError as e:
...   print(e.args[0])
...
42

So, you *can* query it for the missing key, even though it's a bit ugly. But
as far as TOOWTDI goes, I think this is fine as-is. This is just my opinion,
though :)

-- Emanuel


More information about the Python-ideas mailing list