Python doctest 结果死活写不到文件里面去

2017-03-22 10:12:51 +08:00
 SlipStupig

系统:Ubuntu
python:2.7.6
我看别人都能写到文件里面, 我只能输出到终端中,难道我的打开方式有问题
这个是我看的官方实例,也不行


def factorial(n):
    """Return the factorial of n, an exact integer >= 0.

    If the result is small enough to fit in an int, return an int.
    Else return a long.

   >>> # comments are ignored
    >>> # comments are ignored
    >>> x = 12
    >>> x
    
    >>> if x == 13:
    ...     print "yes"
    ... else:
    ...     print "no"
    ...     print "NO"
    ...     print "NO!!!"
    ...


    """

    import math
    if not n >= 0:
        raise ValueError("n must be >= 0")
    if math.floor(n) != n:
        raise ValueError("n must be exact integer")
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")
    result = 1
    factor = 2
    while factor <= n:
        result *= factor
        factor += 1
    return result


if __name__ == "__main__":
    import doctest
    doctest.testmod()

1592 次点击
所在节点    Python
4 条回复
jason0916
2017-03-22 10:27:18 +08:00
我这边试了一下也是不行啊,会不会是搞错了?

我看文档里面的例子的第二部分是执行文件里的测试用例而不是输出结果到文件里,两个部分都试验了一下结果都是输出在终端的
SlipStupig
2017-03-22 11:26:16 +08:00
jason0916
2017-03-22 13:37:18 +08:00
@SlipStupig 文档我有看,你要不试试在命令行加上“> test.log ” 把标准输出定向到文件?
SlipStupig
2017-03-22 14:51:59 +08:00
@jason0916 我关键是想把结果输出到代码里面,方便输出文档

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/349338

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX