您的位置:首页 > 编程语言 > Python开发

python warnings模块的简单应用

2017-08-28 16:17 666 查看
最近在学习Bottle源码时发现用到了warnings相关知识,就认真学习了下,记录下来防止忘记
# -*- coding=utf-8 -*-
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
print 'this is fxn'

fxn()
文件为 fxn.py
运行 python fxn.py
显示 this is fxn 并没有输入警告信息
添加运行参数 -W action
python -W default fxn.py

fxn.py:5: DeprecationWarning: deprecated
warnings.warn("deprecated", DeprecationWarning)
this is fxn
还可以添加error\always\ignore\等参数
默认不加-W action 应该是ignore的,如果用error做action,则不会输出this is fxn,也就是说当成错误来处理,不再向后执行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python warnings