Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Get Docstring Without Going into Interactive Mode

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?

like image 502
Cristian Avatar asked Aug 13 '09 07:08

Cristian


People also ask

How do I get the Python docstring?

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.

Do all functions need docstrings?

Every function you create ought to have a docstring. They're in triple-quoted strings and allow for multi-line text.

Can we use one liner docstring?

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.

Should you document every function?

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.


1 Answers

Any docstring is available through the .__doc__ property:

>>> print str.__doc__

In python 3, you'll need parenthesis for printing:

>>> print(str.__doc__)
like image 177
too much php Avatar answered Sep 20 '22 17:09

too much php