cuốn sách gpt4 ai đã làm

python - 如何使用 : python warnings. warn()?

In lại Tác giả: Walker 123 更新时间:2023-11-28 18:18:38 28 4
mua khóa gpt4 Nike

这是我写的代码:

#usr/bin/python3
import warnings

def tt():

warnings.warn("123")

return 10

x = tt()

print(x)

它打印:

test.py:5: UserWarning: 123

warnings.warn("123")

10

我希望它只打印:

test.py:5: UserWarning: 123
10

KHÔNG warnings.warn("123").

tôi phải làm gì?

câu trả lời hay nhất

您可以替换用于格式化消息的函数;例如:

def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
return '%s:%s: %s:%s\n' % (filename, lineno, category.__name__, message)

warnings.formatwarning = warning_on_one_line

Python Module of the week article

关于python - 如何使用 : python warnings. warn()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46844809/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress