首页 > 后端开发 > Python教程 > Python 中的警告

Python 中的警告

Barbara Streisand
发布: 2024-10-23 08:15:02
原创
779 人浏览过

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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板