In Python, docstrings are used for documenting modules, classes, functions and methods. They are used to provide a summary of a function and explain its usage, arguments and other important details.
Here is an example of a function with a docstring:
main.py322 chars13 lines
As you can see, we enclose the docstring in triple quotes (""") at the beginning of the function. Inside the docstring, we provide a summary of what the function does, what arguments it takes and what it returns.
You can access the docstring of a function using the __doc__
attribute:
main.py27 chars2 lines
This will output the docstring of the my_function
function.
You can also use the help()
function to display the docstring of a function:
main.py18 chars2 lines
This will display the docstring of the my_function
function along with some additional information about the function.
gistlibby LogSnag