首頁  >  文章  >  後端開發  >  Python 中的警告

Python 中的警告

Barbara Streisand
Barbara Streisand原創
2024-10-23 08:15:02685瀏覽

Warning in Python

請我喝杯咖啡☕

警告是警報訊息,它基本上不會引發異常,也不會終止程式。

警告類別如下:

Class Disposition
Warning This is the base class of all warning category classes. It is a subclass of Exception.
UserWarning The default category for warn().
DeprecationWarning Base category for warnings about deprecated features when those warnings are intended for other Python developers (ignored by default, unless triggered by code in __main__).
SyntaxWarning Base category for warnings about dubious syntactic features.
RuntimeWarning Base category for warnings about dubious runtime features.
FutureWarning Base category for warnings about deprecated features when those warnings are intended for end users of applications that are written in Python.
PendingDeprecationWarning Base category for warnings about features that will be deprecated in the future (ignored by default).
ImportWarning Base category for warnings triggered during the process of importing a module (ignored by default).
UnicodeWarning Base category for warnings related to Unicode.
UnicodeWarning Base category for warnings related to Unicode.
BytesWarning Base category for warnings related to bytes and bytearray.
ResourceWarning Base category for warnings related to resource usage (ignored by default).

warn() 可以手動發出警告,如下所示:

*備忘錄:

  • 第一個參數是 message(Required-Type:str)。
  • 第二個參數是類別(可選-預設:無-類型:警告)。 *如果為 None,則將 UserWarning 設定為它。
  • 第三個參數是 stacklevel(可選-預設:1-類型:int)。 *它決定警告所指的程式碼。
  • 第四個參數是來源(Optional-Default:None-Type:Any)。
  • 有skip_file_prefixes參數(可選-預設:無-型別:str的元組): *備註:
    • Skip_file_prefixes=必須使用。
    • 手動設定 None 會出錯。
import warnings

warnings.warn(message="This is a warning.")
# UserWarning: This is a warning.
#   warnings.warn(message="This is a warning.")

warnings.warn(message="This is a warning.",
              category=None,
              stacklevel=1,
              source=None,
              skip_file_prefixes=())
# UserWarning: This is a warning.
#   warnings.warn(message="This is a warning.",

warnings.warn(message="This is a warning.",
              category=Warning)
# Warning: This is a warning.
#   warnings.warn(message="This is a warning.",

warnings.warn(message="This is a warning.",
              category=DeprecationWarning)
# DeprecationWarning: This is a warning.
#   warnings.warn(message="This is a warning.",

def test1():
    warnings.warn(message="Warning 1",
                  stacklevel=-100)
    warnings.warn(message="Warning 2",
                  stacklevel=0)
    warnings.warn(message="Warning 3",
                  stacklevel=1)
    warnings.warn(message="Warning 4",
                  stacklevel=2)
    warnings.warn(message="Warning 5",
                  stacklevel=3)
    warnings.warn(message="Warning 6",
                  stacklevel=4)
    warnings.warn(message="Warning 7",
                  stacklevel=5)
    warnings.warn(message="Warning 8",
                  stacklevel=100)
def test2():
    test1()

def test3():
    test2()
test3()
# UserWarning: Warning 1
#   warnings.warn(message="Warning 1",
# UserWarning: Warning 2
#   warnings.warn(message="Warning 2",
# UserWarning: Warning 3
#   warnings.warn(message="Warning 3",
# UserWarning: Warning 4
#   test1()
# UserWarning: Warning 5
#   test2()
# UserWarning: Warning 6
#   test3()
# UserWarning: Warning 7
#   exec(code_obj, self.user_global_ns, self.user_ns)
# UserWarning: Warning 8

以上是Python 中的警告的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn