cknoll
cknoll

Reputation: 2492

ipython: get the result of `??` (double question mark) magic command as string

The IPython builtin help system says:

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'.

The double question mark magic command (??) thereby prints the type, the docstring and – if available – also the source code of the respective object. I find this information really useful and I want it inside a str-variable (instead of printed directly).

I know thath obj.__doc__ gives the docstring but I guess there is a convenient way to get all the information that ?? produces at once. I look for something like:

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

Is this possible? If so, how?

Upvotes: 2

Views: 996

Answers (1)

Kyon
Kyon

Reputation: 75

you can use "pinfo2", https://ipython.readthedocs.io/en/stable/interactive/magics.html

for example

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")

it will print:

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

Upvotes: 4

Related Questions