gpt4 book ai didi

python - doctests 中的替代结果

转载 作者:太空狗 更新时间:2023-10-30 00:57:48 26 4
gpt4 key购买 nike

我有一个 doctest,我在其中测试 float 转换:

>>> float('fish')

在 Python < 2.7 中,这导致:

ValueError: invalid literal for float(): fish

在 Python 2.7 中结果是

ValueError: could not convert string to float: fish

我可以让这两个结果在我的 doctest 中被接受吗?

最佳答案

您正在寻找 doctest.IGNORE_EXCEPTION_DETAIL选项。该文档有一个很好的示例来说明如何使用它。您还可以使用 ellipsis像通配符一样在 doctest 中保持不变。

像这样的 doctest:

>>> float('fish')
ValueError:...

你可以看到 Alex Martellis 的帖子关于同样的事情 here .

关于python - doctests 中的替代结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4606324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com