Python의 경고

Barbara Streisand
풀어 주다: 2024-10-23 08:15:02
원래의
685명이 탐색했습니다.

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(Optional-Default:1-Type:int)입니다. *경고가 어떤 코드를 가리키는지 결정합니다.
  • 네 번째 인수는 source(Optional-Default:None-Type:Any)입니다.
  • skip_file_prefixes 인수가 있습니다(Optional-Default:None-Type:tuple of str): *메모:
    • Skip_file_prefixes=를 사용해야 합니다.
    • 수동으로 없음을 설정하면 오류가 발생합니다.
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!