ipython:获取"??"的结果(双问号)魔术命令作为字符串



IPython内置帮助系统显示:

Within IPython you have various way to access help:
?         -> Introduction and overview of IPython's features (this screen).
object?   -> Details about 'object'.
object??  -> More detailed, verbose information about 'object'.

双问号魔术命令(??(由此打印相应对象的类型、文档字符串以及(如果可用(源代码。我发现这些信息非常有用,我希望它包含在str-变量中(而不是直接打印(。

我知道hobj.__doc__给出了文档字符串,但我想有一种方便的方法可以同时获得??生成的所有信息。我在找这样的东西:

# pseudo code
from IPython import magic
report = magic.double_question_mark(obj)

这可能吗?如果是,如何?

您可以使用"pinfo2";,https://ipython.readthedocs.io/en/stable/interactive/magics.html

例如

def test(a, b):
import numpy as np
cds = data.range(1000)
cds = cds.random_shuffle()
a = np.array([a])
return a, b

from IPython import get_ipython
ipython = get_ipython()
ipython.run_line_magic("pinfo2", "test")

它将打印:

Signature: test(a, b)
Docstring: <no docstring>
Source:   
def test(a, b):
import numpy as np
cds = data.range(1000)
cds = cds.random_shuffle()
a = np.array([a])
return a, b
File:      ~/PycharmProjects/aib/<ipython-input-14-4adc6bbd758b>
Type:      function

相关内容

最新更新