haishi 发表于 2018-8-13 12:48:16

python warnings模块的简单应用

  最近在学习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,也就是说当成错误来处理,不再向后执行了。
页: [1]
查看完整版本: python warnings模块的简单应用