project_ananthu project_ananthu - 1 year ago 115
Python Question

How to find hidden errors in Python?

This is a simple python code. If the value of

is changed then there will be an error showing that
is undefined. The
condition acting like a hidden condition that only gives the error when the interpreter runs that line of code.

How can I detect these kind of errors in Python?

if a==0 :
print "hello"
else :
print b

Answer Source

pylint is able to find a lot of common (and not so common) errors in python code without executing it, example output from your example:

C:  1, 0: Exactly one space required around assignment
 ^ (bad-whitespace)
C:  2, 0: Exactly one space required around comparison
if a==0 :
    ^^ (bad-whitespace)
C:  2, 0: No space allowed before :
if a==0 :
        ^ (bad-whitespace)
C:  4, 0: No space allowed before :
else :
     ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
E:  5,10: Undefined variable 'b' (undefined-variable)

The line you're interested in here is: E: 5,10: Undefined variable 'b' (undefined-variable)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download