首页 文章

Python doc字符串打印\ n而不是新行(Mac上的Python 2.7.5)

提问于
浏览
3

所以我正在打字

>>> list = [1,2,3]
>>> list.__doc__
"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"

问题在于它字面上打印“\ n”而不是打印新行,使得更复杂的文档字符串几乎无法读取 .

我尝试过的所有其他内置类都会发生同样的事情 .

我打开它时从python获得的版本信息是:darwin上的Python 2.7.5(默认,2013年7月16日,14:23:29)[GCC 4.2.1兼容的Apple LLVM 4.2(clang-425.0.28)]

1 回答

  • 7

    那是因为 list.__doc__ 是一个字符串 .

    当您输入 list.__doc__ 时,您将获得如下内容:

    >>> list.__doc__
    "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"
    

    但是,如果您键入 print list.__doc__

    >>> print list.__doc__
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
    

    因此,print函数正确格式化字符串以包含换行符 .

    希望有帮助:)

相关问题