我有时会遇到以下问题。我有一个函数,它返回一些我感兴趣的东西,打印一些我不关心的东西。例如。
def f(x):
print('Some complicated printing stuff')
important_result = 42
return important_result我想写一个doctest,检查它是否确实返回了正确的结果。但其代码不会被复杂的打印内容混淆。下面这样的东西将会很酷:
def f(x):
"""
>>> f(0)
...
42
"""
print('Some complicated printing stuff')
important_result = 42
return important_result有没有一种优雅的方式来完成这个任务呢?
发布于 2021-10-29 19:59:39
为了获得一些反馈,我最终打印了doctest.testmod()的结果
https://stackoverflow.com/questions/29513866
复制相似问题