In a python script, if we have multiple lists in a single line, for example:
a[1] = b[2] c[3] d[4]
Now, one of these lists throws an error IndexError: List Index Out of Range
because the index is higher than the list length.
Is there a way to improve this default exception handling using try/except
statements such that we can instantly figure out which list caused the problem?
Otherwise, one needs to check each list using a command line debugger. I understand that if an IDE is available, then this feature is probably inbuilt into the IDE.
CodePudding user response:
The easiest way to do this is to artificially split the line over several lines. For example:
b =[1]
c = []
d = [1]
a = b[0] \
c[0] \
d[0]
Traceback (most recent call last):
File "/tmp/foo.py", line 7, in <module>
c[0] \
IndexError: list index out of range
CodePudding user response:
another option could be to use inspect
module to find the line number that raised the error. This requires you to modify the addition
assignment to use local vars previously defined like bv = b[2]
though, or else split it over individual lines as shown.
a = [1, 2, 3]
b = [1] * 3
c = [2] * 3
d = [3] * 5
try:
a[1] = b[2] \
c[3] \
d[4]
except IndexError:
from inspect import trace
var = trace()[0].code_context[0].split('=', 1)[-1].split('[', 1)[0].lstrip(' -*')
print(f'The variable that raised the IndexError was: {var}')
Out:
The variable that raised the IndexError was: c
Interesting fact: the following syntax, which I actually consider good coding practice, actually does not behave how you want for an informative stack trace. I have no idea why, unfortunately.
a[1] = b[2] \
c[3] \
d[4]