登录
首页 >  文章 >  python教程

Python 中的警告

来源:dev.to

时间:2024-12-03 21:30:40 451浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Python 中的警告》,文章讲解的知识点主要包括,如果你对文章方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

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 中的警告》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>