输出警告信息-python cookbook(第3版)高清中文完整版

时间:2024-06-29 23:06:53
【文件属性】:

文件名称:输出警告信息-python cookbook(第3版)高清中文完整版

文件大小:4.84MB

文件格式:PDF

更新时间:2024-06-29 23:06:53

python cookbook 第3版 高清 中文完整版

14.11 输出警告信息 问题 You want to have your program issue warning messages (e.g., about deprecated features or usage problems). 解决方案 To have your program issue a warning message, use the warnings.warn() function. For example: import warnings def func(x, y, logfile=None, debug=False): if logfile is not None: warnings.warn(‘logfile argument deprecated’, DeprecationWarning) ... The arguments to warn() are a warning message along with a warning class, which is typically one of the following: UserWarning, DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, or FutureWarning. The handling of warnings depends on how you have executed the interpreter and other configuration. For example, if you run Python with the -W all option, you’ll get output such as the following: bash % python3 -W all example.py example.py:5: DeprecationWarning: logfile argument is deprecated warnings.warn(‘logfile argument is deprecated’, DeprecationWarning) Normally, warnings just produce output messages on standard error. If you want to turn warnings into exceptions, use the -W error option:


网友评论