Python程序中的警告控制
程序中的警告与错误不同。如果遇到错误,Python程序会立即终止。而警告则是非致命的。它会显示某些消息,但程序会继续运行。发出警告是为了提醒用户某些并非完全是异常的情况。通常,如果发现某些编程元素(如关键字/函数/类等)的某些已弃用的用法,则会显示警告。
警告消息由Python标准库的“警告”模块中定义的warn()函数显示。警告实际上是内置类层次结构中Exception的子类。有很多内置的警告子类。也可以定义用户定义的子类。
| 警告 | 这是所有警告类别类的基类。 |
| UserWarning | warn()的默认类别。 |
| DeprecationWarning | 当这些警告是针对开发人员时,关于已弃用功能的警告。 |
| SyntaxWarning | 关于可疑语法功能的警告。 |
| RuntimeWarning | 关于可疑运行时功能的警告。 |
| FutureWarning | 当这些警告是针对最终用户时,关于已弃用功能的警告。 |
| PendingDeprecationWarning | 关于将来会弃用的功能的警告 |
| ImportWarning | 在导入模块的过程中触发的警告 |
| UnicodeWarning | 与Unicode相关的警告。 |
| BytesWarning | 与字节和字节数组相关的警告。 |
| ResourceWarning | 与资源使用相关的警告。 |
警告示例
以下代码定义了一个包含已弃用方法和计划在该类的未来版本中弃用的方法的类。
# warningexample.py
import warnings
class WarnExample:
def __init__(self):
self.text = "Warning"
def method1(self):
warnings.warn(
"method1 is deprecated, use new_method instead",
DeprecationWarning
)
print ('method1', len(self.text))
def method2(self):
warnings.warn(
"method2 will be deprecated in version 2, use new_method instead",
PendingDeprecationWarning
)
print ('method2', len(self.text))
def new_method(self):
print ('new method', len(self.text))
if __name__=='__main__':
e = WarnExample()
e.method1()
e.method2()
e.new_method()如果从命令提示符执行上述脚本,则为
E:\python37>python warningexample.py
终端上不会显示任何警告消息。为此,您必须使用_Wd开关,如下所示
E:\python37>python -Wd warningexample.py warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 warningexample.py:19: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 new method 7
类似地,以下交互式会话也不会显示任何警告消息。
E:\python37>python >>> from warningexample import WarnExample >>> e = WarnExample() >>> e.method1() method1 7 >>> e.method2() method2 7 >>> e.new_method() new method 7
您必须使用–Wd启动Python会话
E:\python37>python -Wd >>> from warningexample import WarnExample >>> e=WarnExample() >>> e.method1() E:\python37\warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 >>> e.method2() E:\python37\warningexample.py:17: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 >>> e.new_method() new method 7
警告过滤器
警告过滤器控制是忽略警告、显示警告还是将其转换为错误(引发异常)。
| 操作 | 含义 |
|---|---|
| error | 将警告转换为异常。 |
| ignore | 丢弃警告。 |
| always | 始终发出警告。 |
| default | 第一次从每个位置生成警告时打印警告。 |
| module | 第一次从每个模块生成警告时打印警告。 |
| once | 第一次生成警告时打印警告。 |
以下交互式会话通过simplefilter()函数将过滤器设置为default。
E:\python37>python
>>> import warnings
>>> warnings.simplefilter('default')
>>> from warningexample import WarnExample
>>> e=WarnExample()
>>> e.method1()
E:\python37\warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead
DeprecationWarning
method1 7
>>> e.method2()
E:\python37\warningexample.py:17: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead
PendingDeprecationWarning
method2 7
>>> e.new_method()
new method 7为了暂时抑制警告,请将simplefilter设置为“ignore”。
import warnings
def function():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
function()
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP