14.11 Output warning message? problem?
You want your program to generate warning messages (such as obsolete features or usage issues).
Solution?
To output a warning message, you can use a warning.warn()
function. For example:
import warningsdef func ( xylogfile=< Span class= "BP" >nonedebug=false ): if logfile is not none: warnings. Warn ( ' logfile argument deprecated ' , Deprecationwarning)
warn()
parameter is a warning message and a warning class with the following types of warnings: userwarning, deprecationwarning,
Syntaxwarning, runtimewarning, resourcewarning, or futurewarning.
The handling of warnings depends on how you run the interpreter and some other configurations.
For example, if you use the -W all
option to run Python, you will get the following output:
bash % python3 -w all example. Pyexample. Py:5: deprecationwarning: span class= "n" >logfile argument is deprecated Warnings. Warn ( "logfile argument is deprecated ' Deprecationwarning)
In general, warnings are output to standard errors. If you want to tell the warning to be converted to an exception, you can use the -W error
option:
Bash%Python3-WErrorExample.PyTraceback(MostRecentPagerLast):File"Example.py",Line10,Inch<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 deprecatedbash %
Discuss?
When you maintain the software and prompt the user for certain information, but do not need to raise it to an exception level, it is useful to output a warning message.
For example, suppose you are ready to modify the functionality of a function library or framework, you can output a warning message for the part you want to change, and for a period of backwards compatibility.
You can also warn users about how to use the code in a problematic way.
As an example of a warning used by another built-in library, the following shows a warning message that is generated when you destroy a file without closing it:
>>> 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 ' >>>>
By default, not all warning messages will appear. -w
option to control the output of the warning message.
-w all
All warning messages will be output, -w ignore
Ignore all warnings, -w error
Convert warnings to exceptions.
Another option, you can also use warnings.simplefilter ()
function control output.
always
parameters will cause all warning messages to appear " Ignore
Ignore all warnings, error
Converts the warning to an exception.
This is enough for a simple case of generating a warning message.
warnings
The module provides a number of more advanced configuration options for filtering and warning message processing.
For more information, refer to the Python documentation
Albert (http://www.aibbt.com/) The first artificial intelligence portal in China
Python Cookbook (3rd edition) Chinese version: 14.11 Output warning message