What are docstrings and how do I use them to improve my code readability?

Docstrings are strings placed at the beginning of classes, functions, or .py files. They are implemented using three single quotes at the beginning and end. For example:

def Addition(a,b):

''' This function adds a and b together. '''

    return a+b

This docstring is then accessed in the interpreter using "Addition.doc"(no quotes). Docstrings are used to document how to use the class, function or script that it refers to, and so can improve code readability and maintainability.

CW

Related Python Mentoring answers

All answers ▸

Write a python function that takes a string as parameter and returns the character in the string with the most occurrences, along with the number of times this character occurs


Give a segment of python code that will print the numbers 1 to 7, each one on a new line


Write a function that checks whether a number is prime or not.


What is Object-Oriented Programming?