256

I have the following function:

def my_func():
    """My docstring is both funny and informative"""
    pass

How do I get access to the docstring?

S.Lott
  • 384,516
  • 81
  • 508
  • 779
Teifion
  • 108,121
  • 75
  • 161
  • 195
  • 18
    Note that running Python with -OO strips out docstrings. If you intend to distribute your code to others, keep in mind that they may run it that way. It will make those people really really unhappy if your code relies on docstrings, but doesn't catch the case where they don't exist. – DNS Apr 03 '09 at 14:16

4 Answers4

348

Interactively, you can display it with

help(my_func)

Or from code you can retrieve it with (surround it with print(.) to get a formatted output):

my_func.__doc__
Rafs
  • 614
  • 8
  • 19
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 4
    BTW: This technique works with builtin functions as well as modules and classes (test help() with objects too - might also work). This makes your python shell an interactive help shell! – Daren Thomas Apr 03 '09 at 09:24
  • 7
    On the ipython prompt, you can do my_func?? and it would show the docstring as well. – ronak Mar 29 '16 at 16:26
  • 2
    help(func) gives a readable output while func.__doc__ gives a inline output. Thanks for the answer. – Mr. Unnormalized Posterior Jul 11 '18 at 07:54
  • @Mr.UnnormalizedPosterior try `print(func.__doc__)`; I find it more useful while in the CLI, easy to go back to the func's signature when needed. – Rafs Nov 22 '22 at 13:36
105

You can also use inspect.getdoc. It cleans up the __doc__ by normalizing tabs to spaces and left shifting the doc body to remove common leading spaces.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Andrew Dalke
  • 14,889
  • 4
  • 39
  • 54
  • 2
    It cleans it up a bit, but you still get newlines at the original e.g. 80 characters, when the text needs to be printed in a terminal that is wider, e.g. 120 characters. Is there a way to really clean up the `__doc__` ? – xApple Apr 12 '22 at 02:59
  • @xApple The docstring can generally contain lists or blocks of code - things that you want to keep formatted by newlines. If you still want to remove all the newlines in the string, then just remove them, using the [str.replace](https://docs.python.org/3/library/stdtypes.html#str.replace) method. – Jeyekomon Jul 13 '23 at 09:15
13

On ipython or jupyter notebook, you can use all the above mentioned ways, but i go with

my_func?

or

?my_func

for quick summary of both method signature and docstring.

I avoid using

my_func??

(as commented by @rohan) for docstring and use it only to check the source code

sss
  • 598
  • 6
  • 24
-1
import ast
import sys
f = open(sys.argv[1], "r") #filename input
module = ast.parse(f.read())
class_definitions = [node for node in module.body if isinstance(node, ast.ClassDef)]
method_definitions = []
for class_def in class_definitions:
        print(class_def.name)
        print(ast.get_docstring(class_def))
        function_definitions = [node for node in class_def.body if isinstance(node, ast.FunctionDef)]
        for f in function_definitions:
                print('\t---')
                print('\t'+f.name)
                print('\t---')
                print('\t'+'\t'.join(ast.get_docstring(f).splitlines(True)))
        print('----')
  • 8
    Welcome to StackOverflow. This is an interesting answer but it'd be better if you explained a little bit about what this code does and why you'd choose it instead of one of the established answers? – Jack Deeth Feb 07 '22 at 21:10