[IPython-User] pdoc without header

Ludwig Schwardt ludwig.schwardt@gmail....
Mon Nov 28 14:37:36 CST 2011

Hi Tiago,  

> I use IPython to provide a command line user interface to scientists.

We use IPython for exactly the same thing (a command line user interface for astronomers, to be more exact!), and ran into the same issue.

Like Thomas said, you need to modify the pinfo behaviour for your class. We use the inspect_object decorator for this, as demonstrated in the code snippet below. (You would need to change the imports appropriately if you are using IPython 0.10 or earlier, but the functionality is the same.) Simply add an @inspect_object.when_type line for each class you want to override above the _our_doc function. We use the original pinfo for ?? functionality, as it is nice to be able to see the actual source code too.


from IPython.core.interactiveshell import InteractiveShell
from IPython.external.simplegeneric import generic
from IPython.core.page import page

ip_shell = InteractiveShell.instance()

# In order to override default docstring behaviour
# we make pinfo a generic and then use decorators
# in the code to override the docstring behaviour
# when we are looking at one of our classes.
# We keep a reference to the original pinfo in order
# to handle the '.??' case which needs to use the
# original code formatter.
original_pinfo = ip_shell.inspector.pinfo
ip_shell.inspector.pinfo = generic(original_pinfo)
inspect_object = ip_shell.inspector.pinfo

class OurClass(object):
    """This is what we want to see."""

# … add more classes here, e.g. @inspect_object.when_type(AnotherClass)
def _our_doc(obj, oname, formatter, info, **kw):
    if kw['detail_level'] == 1:
        original_pinfo(obj, oname, formatter, info, **kw)
    elif obj.__doc__:

In [2]: ip_shell?    
Type:       TerminalInteractiveShell
Base Class: <class 'IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell'>
String Form:<IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell object at 0x10371e590>
Namespace:  Interactive
File:       /Library/Python/2.7/site-packages/ipython-0.12.dev-py2.7.egg/IPython/frontend/terminal/interactiveshell.py
Docstring:  <no docstring>

In [3]: obj = OurClass()

In [4]: obj?
This is what we want to see.

In [5]: obj??
Type:       OurClass
Base Class: <class '__main__.OurClass'>
String Form:<__main__.OurClass object at 0x1038a39d0>
Namespace:  Interactive
File:       /Library/Python/2.7/site-packages/ipython-0.12.dev-py2.7.egg/IPython/core/fakemodule.py
Docstring:  This is what we want to see.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20111128/ff565d40/attachment.html 

More information about the IPython-User mailing list