2011-08-19 76 views
7

因此,当我尝试打印Python函数function.__doc__的帮助/信息时,在文档字符串中出现\n时,控制台输出而不是打印新行,打印\n。任何人都可以帮助我禁用/帮助与此?python newline display in console

这是我的输出:

'divmod(x, y) -> (div, mod)\n\nReturn the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.' 

我想输出是:

'divmod(x, y) -> (div, mod) 

    Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.' 

P.S:我已经试过这在OS X,Ubuntu的使用Python 2.7。

回答

16

看起来像您检查交互式shell中的对象,而不是打印它。如果你的意思是打印,请写下来。

>>> "abc\n123" 
"abc\n123" 
>>> print "abc\n123" 
abc 
123 

在python 3.x打印是一个普通的函数,所以你必须使用()。下面(推荐)将同时在2.x和3.x工作:

>>> from __future__ import print_function 
>>> print("abc\n123") 
abc 
123 
+0

是。所以根据我的理解,为了'unsee''\ n',我应该使用print来代替只输入[function] .__ doc__? – armundle

+0

是的。它也会放弃报价。 –

3

您可能会发现更多有用的使用(例如)help(divmod)代替divmod.__doc__

1
In [6]: print divmod.__doc__ 
divmod(x, y) -> (div, mod) 

Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x. 

,但我建议你使用

In [8]: help(divmod) 

或IPython的

In [9]: divmod? 
Type:  builtin_function_or_method 
Base Class: <type 'builtin_function_or_method'> 
String Form:<built-in function divmod> 
Namespace: Python builtin 
Docstring: 
divmod(x, y) -> (div, mod) 

Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.