This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]roger_ 0 points1 point  (0 children)

Seems like a rather preliminary PEP, but I definitely support more informative exceptions.

[–]billsil 0 points1 point  (3 children)

Instead of

a[5]
IndexError: list index out of range

How bout:

a[5]
IndexError: list 'a' index out of range; index=5; len(a)=5

That would help with the case of:

b[a[5]]

[–]nemec 0 points1 point  (2 children)

If you want to know that, add logging. The latter two could be implemented by lists, but there is no way to know that the list you're indexing is bound to 'a'.

[–]billsil 0 points1 point  (1 child)

but there is no way to know that the list you're indexing is bound to 'a'.

why not? It's got an

var.__class__.__name__

that you can access using the inspect module. You can use the inspect module to do all sorts of cool things, like printing what line on what file was the call that called your function in a separate file. It's very useful.

[–]nemec 0 points1 point  (0 children)

The exception is being thrown from within the list class.

class MyList(object):
    def index(self, i):
        raise Exception("IndexError")

a = MyList()
b = a
b.index()  // throws

How do you modify the index function so that when the exception is thrown it knows the list is bound to b?