Let's say I want to create dynamic documentation of functions I wrote, without using the help()
function (I don't want the user to care about arguments accepted by my function, nor get cluttered with other stuff like the copyright info, or other stuff that aren't functions).
Here's an attempt:
import inspect
def foo():
"""Return 'foo' string"""
return "foo"
def user_defined_functions():
"""Return names of user-defined functions"""
for name in globals():
if inspect.isfunction(eval(name)):
docstring = eval(name + ".__doc__")
print("{}:\n {}".format(name, docstring))
And here's the pretty result:
>>> import test
>>> test.user_defined_functions()
user_defined_functions:
Return names of user-defined functions
foo:
Return 'foo' string
If I had just used the help function I could have gotten extra stuff I don't need (I'm not writing a library, so the user doesn't need all of this):
Help on module test:
NAME
test
FUNCTIONS
foo()
Return 'foo' string
user_defined_functions()
Return names of user-defined functions
FILE
/home/wena/src/utils/test.py
user_defined_function
? Would you care to explain better? \$\endgroup\$user-defined-functions
, due to a lack of a better one in my head. What I meant by it is functions that aren't built into Python. I am maintaining a CLI application (code.google.com/p/wajig) where there are a bunch of functions in a single file, and each of those represent a subcommand. I need this functionality so that when a user typeswajig commands
, all functions in that file would get displayed with their docstrings. \$\endgroup\$