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()

更新於:2019年7月30日

2K+ 次瀏覽

開啟您的職業生涯

完成課程後獲得認證

開始學習
廣告