The dunderdoc() function, in module dunderdoc.py, is a simple Python introspection tool that I wrote. It prints the __doc__ attribute (the docstring) of each item in a list of names given as argument to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | """
dunderdoc.py
A Python function to print the .__doc__ attribute (i.e. the docstring)
of each item in a list of names given as the argument.
The function is called dunderdoc because it is an informal convention
in the Python community to call attributes such as __name__, that begin
and end with a double underscore, dunder-name, and so on.
Author: Vasudev Ram - http://www.dancingbison.com
Copyright 2015 Vasudev Ram
"""
def dunderdoc(names):
for name in names:
print '-' * 72
print name + '.__doc__:'
print eval(name).__doc__
print '-' * 72
# Call dunderdoc() on some basic objects:
a = 1 # an integer
b = 'abc' # a string
c = False # a boolean
d = () # a tuple
e = [] # a list
f = {} # a dict
g = set() # a set
dunderdoc(('a', 'b', 'c', 'd', 'e', 'f', 'g'))
# Call dunderdoc() on some user-defined objects:
class Foo(object):
"""
A class that implements Foo instances.
"""
def bar(args):
"""
A function that implements bar functionality.
"""
dunderdoc(['Foo', 'bar'])
|
This recipe can be useful to quickly print the docstrings for multiple Python objects at a time, in a readable manner - just pass their names as strings in a list, to the dunderdoc() function.
More information and sample output at this blog post:
http://jugad2.blogspot.in/2015/03/dunderdoc-simple-python-introspection.html