1. 程式人生 > >python warnings模塊的簡單應用

python warnings模塊的簡單應用

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,也就是說當成錯誤來處理,不再向後執行了。

python warnings模塊的簡單應用