Nitpicking - slightly misleading traceback

J

Juho Schultz

if ((data[x][y] > 0) or
(datadict.has_key[key])):

Traceback (most recent call last):
File "reduce.py", line 524, in remove_badvalues
if ((data[x][y] > 0) or
TypeError: unsubscriptable object

However, the bug sits on the next line. I used square brackets when
normal brackets were needed - should have written datadict.has_key(key)
as the old code had datadict[key]. In the real code variable names are
so long that I must split the line. I feel the traceback is misleading.

I guess the best solution is to write code without bugs...
 
S

Sybren Stuvel

Juho Schultz enlightened us with:
However, the bug sits on the next line. [...] I feel the traceback
is misleading.

Well, the bug sits in the command starting on the line indicated.

Nitpick back: Learn about operator precedence and Python syntax rules.
You don't need so many brackets:

if data[x][y] > 0 or datadict.has_key(key):

This might even make things fit on one line again ;-)

Sybren
 
A

Alex Martelli

Sybren Stuvel said:
if data[x][y] > 0 or datadict.has_key(key):

This might even make things fit on one line again ;-)

Particularly if you code it idiomatically:

if data[x][y] > 0 or key in datadict:


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top