ホームページ  >  記事  >  バックエンド開発  >  Python での警告

Python での警告

Barbara Streisand
Barbara Streisandオリジナル
2024-10-23 08:15:02683ブラウズ

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) です。
  • 2 番目の引数は category(Optional-Default:None-Type:Warning) です。 *None の場合、UserWarning が設定されます。
  • 3 番目の引数は stacklevel(Optional-Default:1-Type:int) です。 *警告がどのコードを参照するかを決定します。
  • 4 番目の引数は、source(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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。