Documenting your code
We are big fans of code that does not need documentation. When we write elegant code, following established principles, the code should come out as self-explanatory, with documentation being almost unnecessary. Nevertheless, adding a docstring to a function, or a comment with important information, can be very useful.
You can find the guidelines for documenting Python in PEP 257 – Docstring conventions at https://peps.python.org/pep-0257/, but we will show you the basics here.
Python is documented with strings, which are aptly called docstrings. Any object can be documented, and we can use either one-line or multi-line docstrings. One-liners are very simple. They should not provide another signature for the function, but instead state its purpose:
# docstrings.py
def square(n):
"""Return the square of a number n."""
return n**2
def get_username(userid):
"""Return the username of a user...