Python 相当于 Perl 的“警告”?

bai*_*ley 4 python python-3.x

我正在尝试用 Python 编写一个非常简单的地址簿网络应用程序。warn我是 Python 新手,但已经花了相当多的时间用 Perl 编写,我想知道Python 3.6 中相当于 Perl 函数的是什么。

mat*_*gan 5

如果您正在寻找相当于引发警告的功能,您可以使用 pythonswarnings模块,如下所示:

from warnings import warn

warn('Your message here', Warning)
Run Code Online (Sandbox Code Playgroud)

这将显示__main__:1: Warning: my message here在 stderr 上。

如果你正在寻找相当于引发错误的东西,你可以使用 pythonsException类,如下所示:

raise Exception('Your message here')
Run Code Online (Sandbox Code Playgroud)

这将显示Exception: Your message here在 stderr 上。