[Python-ideas] Generator unpacking
Sven R. Kunze
srkunze at mail.de
Tue Feb 16 05:21:19 EST 2016
On 16.02.2016 01:34, Steven D'Aprano wrote:
> On Mon, Feb 15, 2016 at 01:55:32PM -0800, Andrew Barnert via Python-ideas wrote:
>> On Feb 15, 2016, at 07:13, Sven R. Kunze <srkunze at mail.de> wrote:
>>> And while we are on it, that reminds me of KeyError. I always feel
>>> annoyed by the fact that Python doesn't tell me what key a dict is
>>> missing.
> 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
Sorry. I confused KeyError with IndexError (bracket all over the place).
>>> [1][1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
> Admittedly, the exception object itself doesn't keep a reference to the
> missing key, so you can't programmatically query it for the key, but
> normally that's not a problem since you just tried to look it up so you
> should still have the key.
That's true. However, most the time I see such tracebacks while sifting
through server logs.
So, the more data an exception exposes, the better. I wouldn't even mind
an excerpt of the dict/list itself in order to get a faster
understanding of the problem domain (wrong type of keys, etc.).
>> But for SyntaxError, there is no object to stick in the error object
>> or error message, just a source token or an AST node. Both of those
>> have pointers back to start and end in the source string, and that's
>> what goes into the SyntaxError, which is already shown to the user by
>> printing the relevant line of source and pointing a caret at it.
> Sadly, CPython doesn't manage to even display the caret at all:
>
> [steve at ando ~]$ python -c "[a, 2] = 'xy'"
> File "<string>", line 1
> SyntaxError: can't assign to literal
>
>
>
> Jython gives a much more informative error:
>
> steve at orac:~$ jython -c "[a, 2] = 'ab'"
> File "<string>", line 1
> [a, 2] = 'ab'
> ^
> SyntaxError: can't assign to number
That seems to be a great idea.
Best,
Sven
More information about the Python-ideas
mailing list