>> do_s-6ren">
gpt4 book ai didi

python - 如何在 docutils 中处理空值

转载 作者:太空狗 更新时间:2023-10-29 22:20:00 24 4
gpt4 key购买 nike

我正在尝试在处理空值的函数上运行 doctest。但是 doctest 似乎不喜欢空值......

def do_something_with_hex(c):
"""
>>> do_something_with_hex('\x00')
'\x00'
"""
return repr(c)

import doctest
doctest.testmod()

我看到了这些错误

Failed example:
do_something_with_hex(' ')
Exception raised:
Traceback (most recent call last):
File "C:\Python27\lib\doctest.py", line 1254, in __run
compileflags, 1) in test.globs
TypeError: compile() expected string without null bytes
**********************************************************************

我该怎么做才能在这样的测试用例中允许空值?

最佳答案

您可以转义所有反斜杠,或者将您的文档字符串更改为 raw string literal :

def do_something_with_hex(c):
r"""
>>> do_something_with_hex('\x00')
'\x00'
"""
return repr(c)

如果字符串上有 r 前缀,则反斜杠后面的字符会原封不动地包含在字符串中,并且所有反斜杠都保留在字符串中

关于python - 如何在 docutils 中处理空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710987/

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