Print the docstring for any object. Optional: -formatter: a function to run the docstring through for specially formatted docstrings. Examples -------- In [1]: class NoInit: ...: pass In [2]: class NoDoc: ...:
(self, obj, oname='', formatter=None)
| 417 | # In Python 3, all classes are new-style, so they all have __init__. |
| 418 | @skip_doctest |
| 419 | def pdoc(self, obj, oname='', formatter=None): |
| 420 | """Print the docstring for any object. |
| 421 | |
| 422 | Optional: |
| 423 | -formatter: a function to run the docstring through for specially |
| 424 | formatted docstrings. |
| 425 | |
| 426 | Examples |
| 427 | -------- |
| 428 | |
| 429 | In [1]: class NoInit: |
| 430 | ...: pass |
| 431 | |
| 432 | In [2]: class NoDoc: |
| 433 | ...: def __init__(self): |
| 434 | ...: pass |
| 435 | |
| 436 | In [3]: %pdoc NoDoc |
| 437 | No documentation found for NoDoc |
| 438 | |
| 439 | In [4]: %pdoc NoInit |
| 440 | No documentation found for NoInit |
| 441 | |
| 442 | In [5]: obj = NoInit() |
| 443 | |
| 444 | In [6]: %pdoc obj |
| 445 | No documentation found for obj |
| 446 | |
| 447 | In [5]: obj2 = NoDoc() |
| 448 | |
| 449 | In [6]: %pdoc obj2 |
| 450 | No documentation found for obj2 |
| 451 | """ |
| 452 | |
| 453 | head = self.__head # For convenience |
| 454 | lines = [] |
| 455 | ds = getdoc(obj) |
| 456 | if formatter: |
| 457 | ds = formatter(ds).get('plain/text', ds) |
| 458 | if ds: |
| 459 | lines.append(head("Class docstring:")) |
| 460 | lines.append(indent(ds)) |
| 461 | if inspect.isclass(obj) and hasattr(obj, '__init__'): |
| 462 | init_ds = getdoc(obj.__init__) |
| 463 | if init_ds is not None: |
| 464 | lines.append(head("Init docstring:")) |
| 465 | lines.append(indent(init_ds)) |
| 466 | elif hasattr(obj,'__call__'): |
| 467 | call_ds = getdoc(obj.__call__) |
| 468 | if call_ds: |
| 469 | lines.append(head("Call docstring:")) |
| 470 | lines.append(indent(call_ds)) |
| 471 | |
| 472 | if not lines: |
| 473 | self.noinfo('documentation',oname) |
| 474 | else: |
| 475 | page.page('\n'.join(lines)) |
| 476 |