Why not just show the out-of-range index?

John Machin sjmachin at lexicon.net
Tue Dec 5 01:04:51 EST 2006


OKB (not okblacke) wrote:
> John Machin wrote:
>
> > 3. The OP asked only for values; you are asking for names and
> > values. If you have a magic flak jacket, please let me know; I'd
> > like to borrow it occasionally :-)
>
>     	On reflection I think my alternative suggestion might be just as
> good: the interpreter could indicate the character position within the
> line.  This also handles cases where the operands aren't simple names.
> (Not that I have any idea how much harder/easier this would be to
> implement.)

OTTOMH, it would be easier. The compiler would "only" have to ensure
that the line number and position within the line of each operator was
available at run time (currently keeping only the line number of the
first line in each statement). The cost/benefit ratio would IMHO still
be far too high.

>
>     	I ask for names and values because that is what I want.  If I have
> some expression containing a bunch of variables and an exception is
> raised, the most helpful information for me is the source code token
> that is causing the error, because I can see the source code in my text
> editor.  The value is not as important, because there may be many
> different variables that could be holding the same incorrect value.  I
> only want to look at the one that is ACTUALLY holding the incorrect
> value.
>

Firstly, you may say that you want to look only at the ONE that is
actually bound to THE incorrect value, but the interpreter has in
general no way of telling which one is bad. For example:

foo = "a"
bar = [1]
baz= "z"
goo = [26]
x = foo + bar

This causes: "TypeError: cannot concatenate 'str' and 'list' objects"

Which ONE out of foo and bar is actually bound to THE "incorrect"
value? Did you mean to write "foo + baz", or did you mean to write "goo
+ bar"?

Secondly, if you have so many variables referenced in one statement all
with the same operator (e.g. +) that you have difficulty in working out
where the problem is, I'd say that you have a statement that is far too
long, and you have far too many variables in your function.

Can you give a real example from your code where the location of such a
human error (you cause a variable to bound to a value of inappropriate
type) would be difficult to find, plus an estimate of how often you
would make such an error?

Cheers,
John




More information about the Python-list mailing list