Is it possible to configure Python interpreter to provide more useful info on errors?

Viewed 112

For example, can "IndexError: list index out of range" actually say what value in which variable caused the error and what the bound was? Eg. rather than bare

a[i][j] += max(a[i][j-d], a[i-1][j])
IndexError: list index out of range

Get this:

IndexError: list index out of range: j-d=250 for expected range of [0:250]

This could cut quite some time of the most mundane development process that otherwise I waste on backtracking the code or inserting print statements to obtain such info.

1 Answers

You could use the library better_exceptions.

import better_exceptions

a = [[1, 2], [3, 4]]
i, j, d = 1, 350, 100
a[i][j] += max(a[i][j-d], a[i-1][j])

better_exceptions output

This is not exactly what you are asking for (better_exceptions simply displays all variable values, errors messages are not more explicit), but I think it is a good start.

Related