It appears your teacher is a fan of How to Design Programs ;)

I'd tackle this as writing for two different audiences who won't always overlap.

First there are the docstrings; these are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details (Unless they directly relate to use)

Secondly there are in-code comments; these are to explain what is going on to people (generally you!) who want to extend the code. These will not normally be turned into documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind)
  • Workarounds for code you don't have control over, that may otherwise appear illogical
  • I'll admit to TODOs as well, though I try to keep that to a minimum
  • Where I've made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical

Since you're coding in an academic setting, and it sounds like your lecturer is going for verbose, I'd say just roll with it. Use code comments to explain how you are doing what you say you are doing in the design recipe.

Answer from dejester on Stack Overflow
Top answer
1 of 5
56

It appears your teacher is a fan of How to Design Programs ;)

I'd tackle this as writing for two different audiences who won't always overlap.

First there are the docstrings; these are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details (Unless they directly relate to use)

Secondly there are in-code comments; these are to explain what is going on to people (generally you!) who want to extend the code. These will not normally be turned into documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind)
  • Workarounds for code you don't have control over, that may otherwise appear illogical
  • I'll admit to TODOs as well, though I try to keep that to a minimum
  • Where I've made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical

Since you're coding in an academic setting, and it sounds like your lecturer is going for verbose, I'd say just roll with it. Use code comments to explain how you are doing what you say you are doing in the design recipe.

2 of 5
10

I believe that it's worth to mention what PEP8 says, I mean, the pure concept.

Docstrings

Conventions for writing good documentation strings (a.k.a. "docstrings") are immortalized in PEP 257.

Write docstrings for all public modules, functions, classes, and methods. Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does. This comment should appear after the def line.

PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a line by itself, e.g.:

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.
"""

For one liner docstrings, please keep the closing """ on the same line.

Comments

Block comments

Generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

Paragraphs inside a block comment are separated by a line containing a single #.

Inline Comments

Use inline comments sparingly.

An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

Inline comments are unnecessary and in fact distracting if they state the obvious.

Don't do this:

x = x + 1 # Increment x

But sometimes, this is useful:

x = x + 1 # Compensate for border

Reference

  • https://www.python.org/dev/peps/pep-0008/#documentation-strings
  • https://www.python.org/dev/peps/pep-0008/#inline-comments
  • https://www.python.org/dev/peps/pep-0008/#block-comments
  • https://www.python.org/dev/peps/pep-0257/
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ docstring vs comments
r/learnpython on Reddit: Docstring vs Comments
September 11, 2024 - Docstrings are easily obtainable by other Python tools dynamically just by inspecting your objects. This is useful for tools that do things like generating API documentation. Comments are, by comparison, more difficult for such tools to use in part because comments are discarded by the compiler whereas docstrings are a part of your object (see .__doc__ attribute of any function, class, etc.).
๐ŸŒ
PythonForBeginners
pythonforbeginners.com โ€บ home โ€บ difference between comments and docstrings in python
Difference between comments and docstrings in Python - PythonForBeginners.com
April 14, 2021 - We should keep in mind that comments written using # sign need not follow indentation rules but comments written using multiline strings must follow the indentation of the block in which they are declared. A docstring is a string constant associated with any python object or module.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Docstring vs. Comment - Python Help - Discussions on Python.org
October 20, 2021 - I hope yโ€™all donโ€™t get annoyed with me. Iโ€™m wondering whatโ€™s the difference between a doctoring and a comment at the beginning of a program. For example: ''' This program prints a user's age ''' vs # This program prinโ€ฆ
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what is the difference between a __docstring__ and a comment?
r/learnpython on Reddit: What is the difference between a __docstring__ and a comment?
December 19, 2017 - And therefore, docstrings are for documentation that is designed to be read by users of your code, while comments are for people who are modifying it. ... A comment is completely ignored by Python whereas a docstring is a string literal that ...
๐ŸŒ
Jaraco
blog.jaraco.com โ€บ why-docstrings-are-preferable-to-comments
In Python, use docstrings or comments? - Jason R. Coombs
January 1, 2022 - In particular, docstrings are recommended to use triple quotes, even when the docstring is a single line, in order to facilitate easy editing to include multiple lines. In contrast, comments in Python follow the shell-style comments that only apply to a single line.
๐ŸŒ
PythonForBeginners
pythonforbeginners.com โ€บ home โ€บ when to use comments vs. docstrings in python
When to Use Comments vs. Docstrings in Python - PythonForBeginners.com
July 27, 2021 - String comments can be many lines long. Python will ignore them when you run the program. """ With string comments, thereโ€™s no limit on how long your comment can be. But there is a need for caution. String comments can be mistaken for docstrings if you put them in the wrong place.
Top answer
1 of 2
21

PEP 8 -- Style Guide for Python Code categories comments and document strings (a.k.a. docstrings) under comments sections.

Comments

  1. Block Comments
  2. Inline Comments
  3. Documentation Strings

Block comments generally apply to some (or all) code that follows them and are indented to the same level as that code.

Inline comments are unnecessary and in fact distracting if they state the obvious.

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.


Now to answer your question

Docstrings are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details (Unless they directly relate to use).

In-code comments are to explain what is going on to people those who want to extend the code. These will not normally be turned into the documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal (credits) rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind).
  • Workarounds for the code you don't have control over, that may otherwise appear illogical.
  • I'll admit to TODOs as well, though I try to keep that to a minimum.
  • Where I've made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical.
2 of 2
9

Code comments and docstrings have different purposes and audiences:

  • Developers write docstrings to describe the function's behaviour. Other developers, who use this function, read docstrings to find about the meaning of parameters, the pre- and post-conditions, possible exceptions etc.

    If you're writing an API, you may want to publish docstrings as a part of documentation, but have your code and code comments private.

  • Developers write comments to describe the code's inner logic, when this logic isn't clear from just reading the code. The audience is themselves and other developers, who will modify this function in the future.

Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @shubhanshusharma2193 โ€บ doctring-v-s-comments-in-py-f36fe0d25986
Doctring V/S Comments in Py. Both docstrings and comments are twoโ€ฆ | by pyguy | Medium
April 15, 2024 - In summary, docstrings are used for documentation purposes and follow specific conventions, while comments are used for annotating code and making it more readable for other developers.
๐ŸŒ
Prodigiouspython
prodigiouspython.github.io โ€บ ProdigiousPython โ€บ prodigiouspython โ€บ Chapter_3 โ€บ 1_Comments_and_docstrings.html
14. Comments and Docstrings โ€” Prodigious Python ๐Ÿ
Docstrings are written similar to the multi-line comments using """ """ or ''' ''', the only difference would be they are written exactly at the start(first statement) of the module, class, method or function.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1418335 โ€บ whats-the-difference-between-comments-and-docstring-in-python
What's the difference between comments and docstring in python? | Sololearn: Learn to code for FREE!
e.g Docstring= '''ksjsddkdkkdjd snnsjdjdjd snjkdkdkdhdjdkkddj''' comment= #jkssjbddkdk ... There's not much difference when you're normally running the code. But docstrings can be accessed from outside the file using the help() function.
๐ŸŒ
Shefali
learnify.shefali.dev โ€บ tutorials โ€บ python-comments-vs-docstrings
Python Comments vs Docstrings | Learnify
Use comments to explain the "how" and docstrings to explain the "what and why" of code functionality.
๐ŸŒ
Hack FAQs
hackfaqs.com โ€บ docstrings-vs-comments
What's the difference between docstrings and comments in python code?
January 19, 2025 - Docstrings are wrapped in quotes (either """ or ''' and need to be placed immediately after an object definition. Comments are more brief. Think of them like quick "note to self" items that a main developer wants to have top of mind.
๐ŸŒ
Quora
quora.com โ€บ When-should-I-use-comments-vs-docstrings-in-Python
When should I use comments vs. docstrings in Python? - Quora
Answer (1 of 5): Docstrings should be at the start of each module and immediately follow each method, function and class definition. Docstrings are a critical part of your code documentation - well defined doc strings should read like the reference page for the module, class, method or function....
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3085174 โ€บ what-is-the-difference-between-regular-comments-and-docstring-in-python
what is the difference between regular comments and docstring in python? | Sololearn: Learn to code for FREE!
pythoncommentsdocstringregular-comments ... on your program. docstring : used for multiline comment Docstrings provide documentation about functions, classes, and modules....
๐ŸŒ
University of Vermont
uvm.edu โ€บ ~cbcafier โ€บ cs1210 โ€บ book โ€บ 06_style โ€บ comments.html
Comments and docstrings โ€“ Clayton Cafiero
Python uses the # (call it what ... are some examples: # This is a single-line comment foo = 'bar' # This is an inline comment ยท Docstring is short for documentation string....
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Docstrings act as documentation for the class, module, and packages. On the other hand, Comments are mainly used to explain non-obvious portions of the code and can be useful for comments on Fixing bugs and tasks that are needed to be done.
๐ŸŒ
Medium
mohammed-arshan.medium.com โ€บ python-single-line-vs-multi-line-vs-doc-string-7888737625a9
Python : Single Line Comment vs Multi Line Comment vs DocString | by Mohammed Arshan Jada | Medium
January 31, 2022 - In general, it is a good idea to write comments while you are writing or updating a program as it is easy to forget your thought process later on, and comments written later may be less useful in the long term. These not only help other programmers working on the same project but the testers can also refer them for clarity on white-box testing. Docstring is short for documentation string. It is not ignored by the Python interpreter.