I want to grab the docstring in my commandline application, but every time I call the builtin help() function, Python goes into interactive mode.
How do I get the docstring of an object and not have Python grab focus?
Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function. An object's docstring is defined by including a string constant as the first statement in the object's definition.
Every function you create ought to have a docstring. They're in triple-quoted strings and allow for multi-line text.
Per convention, you use one-line docstrings if the function, module, class, or method is obvious enough to warrant a short explanation—but nothing more. You can enclose the one-liner docstring within single quotes, double quotes, or even triple quotes.
Yes, you should always document functions. Many answers write about commenting your code, this is very different. I say about docstrings, which document your interface.
Any docstring is available through the .__doc__
property:
>>> print str.__doc__
In python 3, you'll need parenthesis for printing:
>>> print(str.__doc__)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With