Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

docstring - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...