5
5
----------
6
6
问题
7
7
----------
8
- You want to have your program issue warning messages (e.g., about deprecated features
9
- or usage problems).
8
+ 你希望自己的程序能生成警告信息(比如废弃特性或使用问题)。
10
9
11
10
|
12
11
13
12
----------
14
13
解决方案
15
14
----------
16
- To have your program issue a warning message, use the warnings.warn() function. For
17
- example:
15
+ 要输出一个警告消息,可使用 ``warning.warn() `` 函数。例如:
18
16
19
- import warnings
17
+ .. code-block :: python
20
18
21
- def func(x, y, logfile=None, debug=False):
22
- if logfile is not None:
23
- warnings.warn('logfile argument deprecated', DeprecationWarning)
24
- ...
19
+ import warnings
25
20
26
- The arguments to warn() are a warning message along with a warning class, which is
27
- typically one of the following: UserWarning, DeprecationWarning, SyntaxWarning,
28
- RuntimeWarning, ResourceWarning, or FutureWarning.
29
- The handling of warnings depends on how you have executed the interpreter and other
30
- configuration. For example, if you run Python with the -W all option, you’ll get output
31
- such as the following:
21
+ def func (x , y , logfile = None , debug = False ):
22
+ if logfile is not None :
23
+ warnings.warn(' logfile argument deprecated' , DeprecationWarning )
24
+ ...
25
+
26
+ ``warn() `` 的参数是一个警告消息和一个警告类,警告类有如下几种:UserWarning, DeprecationWarning,
27
+ SyntaxWarning, RuntimeWarning, ResourceWarning, 或 FutureWarning.
28
+
29
+ 对警告的处理取决于你如何运行解释器以及一些其他配置。
30
+ 例如,如果你使用 ``-W all `` 选项去运行Python,你会得到如下的输出:
31
+
32
+ ::
32
33
33
34
bash % python3 -W all example.py
34
35
example.py:5: DeprecationWarning: logfile argument is deprecated
35
36
warnings.warn('logfile argument is deprecated', DeprecationWarning)
36
37
37
- Normally, warnings just produce output messages on standard error. If you want to turn
38
- warnings into exceptions, use the -W error option:
38
+ 通常来讲,警告会输出到标准错误上。如果你想讲警告转换为异常,可以使用 ``-W error `` 选项:
39
+
40
+ ::
39
41
40
42
bash % python3 -W error example.py
41
43
Traceback (most recent call last):
@@ -51,31 +53,27 @@ warnings into exceptions, use the -W error option:
51
53
----------
52
54
讨论
53
55
----------
54
- Issuing a warning message is often a useful technique for maintaining software and
55
- assisting users with issues that don’t necessarily rise to the level of being a full-fledged
56
- exception. For example, if you’re going to change the behavior of a library or framework,
57
- you can start issuing warning messages for the parts that you’re going to change while
58
- still providing backward compatibility for a time. You can also warn users about prob‐
59
- lematic usage issues in their code.
60
- As another example of a warning in the built-in library, here is an example of a warning
61
- message generated by destroying a file without closing it:
62
-
63
- >>> import warnings
64
- >>> warnings.simplefilter(' always' )
65
- >>> f = open (' /etc/passwd' )
66
- >>> del f
67
- __main__:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/passwd'
68
- mode='r' encoding='UTF-8'>
69
- >>>
70
-
71
- By default, not all warning messages appear. The -W option to Python can control the
72
- output of warning messages. -W all will output all warning messages, -W ignore
73
- ignores all warnings, and -W error turns warnings into exceptions. As an alternative,
74
- you can can use the warnings.simplefilter() function to control output, as just
75
- shown. An argument of always makes all warning messages appear, ignore ignores all
76
- warnings, and error turns warnings into exceptions.
77
- For simple cases, this is all you really need to issue warning messages. The warnings
78
- module provides a variety of more advanced configuration options related to the fil‐
79
- tering and handling of warning messages. See the Python documentation for more
80
- information.
56
+ 在你维护软件,提示用户某些信息,但是又不需要将其上升为异常级别,那么输出警告信息就会很有用了。
57
+ 例如,假设你准备修改某个函数库或框架的功能,你可以先为你要更改的部分输出警告信息,同时向后兼容一段时间。
58
+ 你还可以警告用户一些对代码有问题的使用方式。
59
+
60
+ 作为另外一个内置函数库的警告使用例子,下面演示了一个没有关闭文件就销毁它时产生的警告消息:
61
+
62
+ .. code-block :: python
63
+
64
+ >> > import warnings
65
+ >> > warnings.simplefilter(' always' )
66
+ >> > f = open (' /etc/passwd' )
67
+ >> > del f
68
+ __main__:1 : ResourceWarning : unclosed file < _io.TextIOWrapper name= ' /etc/passwd'
69
+ mode= ' r' encoding= ' UTF-8' >
70
+ >> >
71
+
72
+ 默认情况下,并不是所有警告消息都会出现。``-W `` 选项能控制警告消息的输出。
73
+ ``-W all `` 会输出所有警告消息,``-W ignore `` 忽略掉所有警告,``-W error `` 将警告转换成异常。
74
+ 另外一种选择,你还可以使用 ``warnings.simplefilter() `` 函数控制输出。
75
+ ``always `` 参数会让所有警告消息出现,```ignore `` 忽略调所有的警告,``error `` 将警告转换成异常。
81
76
77
+ 对于简单的生成警告消息的情况这些已经足够了。
78
+ ``warnings `` 模块对过滤和警告消息处理提供了大量的更高级的配置选项。
79
+ 更多信息请参考 `Python文档 <https://docs.python.org/3/library/warnings.html >`_
0 commit comments