Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print(__doc__) in Python 3 script

I can't figure out what does the print(__doc__) do at the beginning of a script, like in this Scikit example.

I have been looking for Python docstrings in google, and it seems __doc__ is useful to provide some documentation in, say, functions. But I can't see what does __doc__ do in the middle of a script.

like image 524
Tanguy Avatar asked Oct 11 '15 14:10

Tanguy


People also ask

What is print (__ doc __) in Python?

The print(__doc__) command simply re-uses that documentation string to write it to your terminal each time you run the script, and any other python tool (like the interactive interpreter help() function, for example) can introspect that same value.

What is the use of __ doc __ in Python?

What does built-in class attribute __doc__ do in Python? All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code. This is an example of how a doc_string looks like. This string gives useful information about the function being defined.

How do you write a Docstring in Python?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

Can you print functions in Python?

Print FunctionThe Python print() function takes in any number of parameters, and prints them out on one line of text.


Video Answer


2 Answers

it seems __doc__ is useful to provide some documentation in, say, functions

This is true. In addition to functions, documentation can also be provided in modules. So, if you have a file named mymodule.py like this:

"""This is the module docstring."""

def f(x):
    """This is the function docstring."""
    return 2 * x

You can access its docstrings like this:

>>> import mymodule
>>> mymodule.__doc__
'This is the module docstring.'
>>> mymodule.f.__doc__
'This is the function docstring.'

Now, back to your question: what does print(__doc__) do? Simply put: it prints the module docstring. If no docstring has been specified, __doc__ defaults to None.

like image 121
Andrea Corbellini Avatar answered Oct 17 '22 03:10

Andrea Corbellini


Any function, class or module starting with a string literal has a non-empty __doc__; that initial string is taken as the documentation string; it'll be set to None if no such string is present. See the docstring term definition in the Python glossary.

When you download that Scikit script example, you'll see it starts with such a string:

"""
================================
Recognizing hand-written digits
================================

An example showing how the scikit-learn can be used to recognize images of
hand-written digits.

This example is commented in the
:ref:`tutorial section of the user manual <introduction>`.

"""

The print(__doc__) command simply re-uses that documentation string to write it to your terminal each time you run the script, and any other python tool (like the interactive interpreter help() function, for example) can introspect that same value.

like image 25
Martijn Pieters Avatar answered Oct 17 '22 03:10

Martijn Pieters