I’m using python 3.1.
Is it possible to create more than 1 docstring for a single module or function?
I’m creating a program, and I’m intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-programmers alike, I’m putting a reference to the docstring for documentation within the program itself.
To be more specific, I have a menu in the program/module as an interface, and one of the options will allow access to the module docstring for documentation on the program. Thus, if it’s possible, I want to make multiple docstrings to categorise different types of documentation. So it would be easier on the users if they want to see some part of the documentation.
eg. first docstring contains instructions on how to use the program. Second docstring contains information on how one part of the program works. Third docstring contains info on how another part works. etc.
Is this possible? And if so, how do you reference them?
Updates: Added a comment.
My original thought was to actually have more than one docstring in the sense of:
def foo():
"""docstring1: blah blah blah"""
"""docstring2: blah blah blah"""
pass # Insert code here
Then there would be some code that I could use to allow me to reference each of these docstrings.
So, I’m guessing that this isn’t possible then?
I don’t recommend trying to do something complicated with the docstrings. Best to keep the docstrings simple, and do something else if you want to make a bunch of different documentation options available.
If you really want to do what you described, I suggest you use tags to delimit sections within docstrings. Like so:
Then you can pretty easily split your string into chunks, and when the user chooses a menu item, show just the appropriate chunks.
This way, when you are working in the interactive Python shell, you can say “help(foo)” and you will see all the docstrings. And, you are not changing the fundamental behavior of a basic part of Python, which would freak out other people trying to study your code.
You could also do something even simpler: just make a big global dictionary of docstrings for different purposes, and update it from the source code for each new thing.
doc_developers = {}
doc_testers = {}
The biggest thing I don’t like about this is that, if you change the name of function foo, you would need to change it in multiple places: once in the actual
defand once per dictionary update line. But you could mostly fix that by writing a function:There is probably a way to turn doc_update() into a decorator, but I’m out of time right now.