开发者社区> 问答> 正文

输出警告信息

你希望自己的程序能生成警告信息(比如废弃特性或使用问题)。

展开
收起
哦哦喔 2020-04-17 17:35:29 1825 0
2 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    在你维护软件,提示用户某些信息,但是又不需要将其上升为异常级别,那么输出警告信息就会很有用了。 例如,假设你准备修改某个函数库或框架的功能,你可以先为你要更改的部分输出警告信息,同时向后兼容一段时间。 你还可以警告用户一些对代码有问题的使用方式。

    作为另外一个内置函数库的警告使用例子,下面演示了一个没有关闭文件就销毁它时产生的警告消息:

    import warnings warnings.simplefilter('always') f = open('/etc/passwd') del f main:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>

    默认情况下,并不是所有警告消息都会出现。-W 选项能控制警告消息的输出。 -W all 会输出所有警告消息,-W ignore 忽略掉所有警告,-W error 将警告转换成异常。 另外一种选择,你还可以使用 warnings.simplefilter() 函数控制输出。 always 参数会让所有警告消息出现,`ignore 忽略调所有的警告,error 将警告转换成异常。

    对于简单的生成警告消息的情况这些已经足够了。 warnings 模块对过滤和警告消息处理提供了大量的更高级的配置选项

    2020-04-17 17:59:29
    赞同 展开评论 打赏
  • 输出一个警告消息,可使用 warning.warn() 函数。例如:
    
    import warnings
    
    def func(x, y, logfile=None, debug=False):
        if logfile is not None:
             warnings.warn('logfile argument deprecated', DeprecationWarning)
        ...
    warn() 的参数是一个警告消息和一个警告类,警告类有如下几种:UserWarning, DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, 或 FutureWarning.
    
    对警告的处理取决于你如何运行解释器以及一些其他配置。 例如,如果你使用 -W all 选项去运行Python,你会得到如下的输出:
    
    bash % python3 -W all example.py
    example.py:5: DeprecationWarning: logfile argument is deprecated
      warnings.warn('logfile argument is deprecated', DeprecationWarning)
    通常来讲,警告会输出到标准错误上。如果你想讲警告转换为异常,可以使用 -W error 选项:
    
    bash % python3 -W error example.py
    Traceback (most recent call last):
      File "example.py", line 10, in <module>
        func(2, 3, logfile='log.txt')
      File "example.py", line 5, in func
        warnings.warn('logfile argument is deprecated', DeprecationWarning)
    DeprecationWarning: logfile argument is deprecated
    bash %
    
    2020-04-17 17:35:38
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载