在不中断程序的情况下在 Python 中引发警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3891804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Raise warning in Python without interrupting program
提问by Tomas Novotny
I am trying to raise a Warning in Python without making the program crash / stop / interrupt.
我试图在不使程序崩溃/停止/中断的情况下在 Python 中引发警告。
I use the following simple function to check if the user passed a non-zero number to it. If so, the program should warn them, but continue as per normal. It should work like the code below, but should use class Warning(), Error()or Exception()instead of printing the warning out manually.
我使用以下简单函数来检查用户是否向其传递了非零数字。如果是这样,程序应该警告他们,但继续照常进行。它应该像下面的代码一样工作,但应该使用 class Warning(),Error()或者Exception()而不是手动打印警告。
def is_zero(i):
if i != 0:
print "OK"
else:
print "WARNING: the input is 0!"
return i
If I use the code below and pass 0 to the function, the program crashes and the value is never returned. Instead, I want the program to continue normally and just inform the user that he passed 0 to the function.
如果我使用下面的代码并将 0 传递给函数,程序将崩溃并且永远不会返回该值。相反,我希望程序继续正常运行,只是通知用户他将 0 传递给函数。
def is_zero(i):
if i != 0:
print "OK"
else:
raise Warning("the input is 0!")
return i
I want to be able to test that a warning has been thrown testing it by unittest. If I simply print the message out, I am not able to test it with assertRaises in unittest.
我希望能够测试 unittest 是否抛出了警告来测试它。如果我只是将消息打印出来,则无法在 unittest 中使用 assertRaises 对其进行测试。
采纳答案by SilentGhost
回答by necromancer
回答by Acumenus
By default, unlike an exception, a warning doesn't interrupt.
默认情况下,与异常不同,警告不会中断。
After import warnings, it is possible to specify a Warningsclass when generating a warning. If one is not specified, it is literally UserWarningby default.
之后import warnings,可以在生成警告时指定警告类。如果未指定,则UserWarning默认为字面意思。
>>> warnings.warn('This is a default warning.')
<string>:1: UserWarning: This is a default warning.
To simply use a preexisting class instead, e.g. DeprecationWarning:
要简单地使用预先存在的类,例如DeprecationWarning:
>>> warnings.warn('This is a particular warning.', DeprecationWarning)
<string>:1: DeprecationWarning: This is a particular warning.
Creating a custom warning class is similar to creating a custom exception class:
创建自定义警告类类似于创建自定义异常类:
>>> class MyCustomWarning(UserWarning):
... pass
...
... warnings.warn('This is my custom warning.', MyCustomWarning)
<string>:1: MyCustomWarning: This is my custom warning.
For testing, consider assertWarnsor assertWarnsRegex.
对于测试,请考虑assertWarns或assertWarnsRegex。
As an alternative, especially for standalone applications, consider the loggingmodule. It can log messages having a level of debug, info, warning, error, etc. Log messages having a level of warningor higher are by default printed to stderr.
作为替代方案,尤其是对于独立应用程序,请考虑logging模块。它可以记录具有debug、info、warning、error等级别的消息。具有警告级别或更高级别的日志消息默认打印到 stderr。

