I have written a module, which should only be used by means of import, then call the functions as required.
I do not want this module to do anything when ran interactively, other than printing out the docstrings of all the functions, which have docstrings, and which are not starting with '_'
I am looking for tips/tricks to improve this code. Is there an alternative way, as I am not very skilled in the inspect
module nor in sys.modules
:
import sys
import inspect
def function1():
"""
docstring of function1
usage:
import thismodule
a = thismodule.function1()
"""
pass
def function2():
"""
docstring of function2
usage:
import thismodule
a = thismodule.function2()
"""
pass
def function3():
#this function has not docstring
#therefor should not be printed
pass
def _function4():
"""
this function has a docstring
but the name starts with _ indicating an internal funciton
this should not be printed
"""
pass
def main():
for name, obj in inspect.getmembers(sys.modules[__name__]):
if (
(inspect.isfunction(obj)) ## only functions
and (obj.__module__ == __name__) ## only for this module
and (name[0:1] != '_') ## no internals
and (obj.__doc__ != None) ## __doc__ must be defined
):
print('****************************')
print('function: ' + name + '()')
print(obj.__doc__)
print('****************************')
if __name__ == '__main__':
main()
pydoc
utility from the command line or thehelp()
function from within the Python shell. For example, if your module is saved in a filemymodule.py
, runpydoc mymodule
from the directory where the file is located. You should read docs.python.org/2/library/pydoc.html for more information. \$\endgroup\$