Show More
@@ -31,6 +31,7 b' except ImportError:' | |||||
31 | # IPython's own |
|
31 | # IPython's own | |
32 | from IPython.core import page |
|
32 | from IPython.core import page | |
33 | from IPython.lib.pretty import pretty |
|
33 | from IPython.lib.pretty import pretty | |
|
34 | from IPython.testing.skipdoctest import skip_doctest | |||
34 | from IPython.utils import PyColorize |
|
35 | from IPython.utils import PyColorize | |
35 | from IPython.utils import openpy |
|
36 | from IPython.utils import openpy | |
36 | from IPython.utils import py3compat |
|
37 | from IPython.utils import py3compat | |
@@ -435,6 +436,66 b' class Inspector(Colorable):' | |||||
435 | else: |
|
436 | else: | |
436 | print(header,self.format(output), end=' ') |
|
437 | print(header,self.format(output), end=' ') | |
437 |
|
438 | |||
|
439 | # In Python 3, all classes are new-style, so they all have __init__. | |||
|
440 | @skip_doctest | |||
|
441 | def pdoc(self, obj, oname='', formatter=None): | |||
|
442 | """Print the docstring for any object. | |||
|
443 | ||||
|
444 | Optional: | |||
|
445 | -formatter: a function to run the docstring through for specially | |||
|
446 | formatted docstrings. | |||
|
447 | ||||
|
448 | Examples | |||
|
449 | -------- | |||
|
450 | ||||
|
451 | In [1]: class NoInit: | |||
|
452 | ...: pass | |||
|
453 | ||||
|
454 | In [2]: class NoDoc: | |||
|
455 | ...: def __init__(self): | |||
|
456 | ...: pass | |||
|
457 | ||||
|
458 | In [3]: %pdoc NoDoc | |||
|
459 | No documentation found for NoDoc | |||
|
460 | ||||
|
461 | In [4]: %pdoc NoInit | |||
|
462 | No documentation found for NoInit | |||
|
463 | ||||
|
464 | In [5]: obj = NoInit() | |||
|
465 | ||||
|
466 | In [6]: %pdoc obj | |||
|
467 | No documentation found for obj | |||
|
468 | ||||
|
469 | In [5]: obj2 = NoDoc() | |||
|
470 | ||||
|
471 | In [6]: %pdoc obj2 | |||
|
472 | No documentation found for obj2 | |||
|
473 | """ | |||
|
474 | ||||
|
475 | head = self.__head # For convenience | |||
|
476 | lines = [] | |||
|
477 | ds = getdoc(obj) | |||
|
478 | if formatter: | |||
|
479 | ds = formatter(ds).get('plain/text', ds) | |||
|
480 | if ds: | |||
|
481 | lines.append(head("Class docstring:")) | |||
|
482 | lines.append(indent(ds)) | |||
|
483 | if inspect.isclass(obj) and hasattr(obj, '__init__'): | |||
|
484 | init_ds = getdoc(obj.__init__) | |||
|
485 | if init_ds is not None: | |||
|
486 | lines.append(head("Init docstring:")) | |||
|
487 | lines.append(indent(init_ds)) | |||
|
488 | elif hasattr(obj,'__call__'): | |||
|
489 | call_ds = getdoc(obj.__call__) | |||
|
490 | if call_ds: | |||
|
491 | lines.append(head("Call docstring:")) | |||
|
492 | lines.append(indent(call_ds)) | |||
|
493 | ||||
|
494 | if not lines: | |||
|
495 | self.noinfo('documentation',oname) | |||
|
496 | else: | |||
|
497 | page.page('\n'.join(lines)) | |||
|
498 | ||||
438 | def psource(self, obj, oname=''): |
|
499 | def psource(self, obj, oname=''): | |
439 | """Print the source code for an object.""" |
|
500 | """Print the source code for an object.""" | |
440 |
|
501 |
General Comments 0
You need to be logged in to leave comments.
Login now