Python 如何在未执行的代码中查找异常?
在这个简单的Python代码中,如果Python 如何在未执行的代码中查找异常?,python,Python,在这个简单的Python代码中,如果a的值从0更改,则会出现namererror显示b未定义。但是,如果从未执行else子句,则错误将保持隐藏状态。如何检测此类错误 a=0 if a==0 : print "hello" else : print b 能够在python代码中发现许多常见(但不太常见)错误,而无需执行,示例输出来自您的示例: C: 1, 0: Exactly one space required around assignment a=0 ^ (bad-whites
a
的值从0
更改,则会出现namererror
显示b
未定义。但是,如果从未执行else
子句,则错误将保持隐藏状态。如何检测此类错误
a=0
if a==0 :
print "hello"
else :
print b
能够在python代码中发现许多常见(但不太常见)错误,而无需执行,示例输出来自您的示例:
C: 1, 0: Exactly one space required around assignment
a=0
^ (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)
>>> a = 1
>>> if a == 0:
... print a
... else:
... try:
... print b
... except Exception as e:
... print 'Caught Exception: ', e # where e is the exception string
...
Caught Exception: name 'b' is not defined
这里您感兴趣的一行是:E:5,10:未定义变量'b'(未定义变量)
能够在python代码中找到许多常见(不太常见)错误,而无需执行它,示例输出如下:
C: 1, 0: Exactly one space required around assignment
a=0
^ (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)
>>> a = 1
>>> if a == 0:
... print a
... else:
... try:
... print b
... except Exception as e:
... print 'Caught Exception: ', e # where e is the exception string
...
Caught Exception: name 'b' is not defined
您感兴趣的行是:
E:5,10:未定义变量“b”(未定义变量)
有两种方法可以实现这一点:
C: 1, 0: Exactly one space required around assignment
a=0
^ (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)
>>> a = 1
>>> if a == 0:
... print a
... else:
... try:
... print b
... except Exception as e:
... print 'Caught Exception: ', e # where e is the exception string
...
Caught Exception: name 'b' is not defined
使用还可用于获取与引发的异常相关的所有信息
有两种方法可以实现这一点:
C: 1, 0: Exactly one space required around assignment
a=0
^ (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)
>>> a = 1
>>> if a == 0:
... print a
... else:
... try:
... print b
... except Exception as e:
... print 'Caught Exception: ', e # where e is the exception string
...
Caught Exception: name 'b' is not defined
使用还可用于获取与引发的异常相关的所有信息
如果
a
定义为a=0
(如OP示例所示),pylint将捕获此类错误创建a以抑制错误并打印/写入以记录异常,else
子句将永远不会执行如果a
定义为a=0
(如OP示例中所示),则将永远不会执行else
子句