Code Comments and Readability
A docstring is a documentation string that Python stores as part of a function object, making it accessible during program execution—unlike comments, which are ignored.
Documentation with Two Different Paths
When you read a function, you need to understand what it does without reconstructing every line of its implementation. Python provides two ways to leave explanatory text in source code: comments and docstrings. Both can help a human reader, but Python treats them differently. A comment is ignored, while a docstring becomes part of the function object and remains available while the program runs.
What Survives Source Reading
A comment is a line of text preceded by the hash symbol. Python ignores it completely, so the running program cannot retrieve that comment. A docstring is different: it is a string literal placed immediately after the function definition line. Python stores that documentation as part of the function object.
| Feature | Comment | Docstring |
|---|---|---|
| Written as | Text preceded by # | A string literal immediately after the function definition line |
| Stored by Python | No | Yes, as part of the function object |
| Accessible during execution | No | Yes, through __doc__ or help() |
| Useful to tools | Not available to the running program | Can support IDE tooltips and documentation generators |
Writing a Function Docstring
Place the docstring immediately after the function definition line. You can write it with single quotes, double quotes, or triple quotes. Triple quotes are preferred for multi-line documentation because they allow the documentation to span multiple lines without requiring escaped newlines.
From Definition to Runtime Access
During execution, Python makes the function available as a function object. Because the documentation string was placed correctly, the function object stores it in the special __doc__ attribute. You can inspect that attribute directly, or ask help() to display documentation based on the docstring.
Inspecting a function's documentation
Write a function with a docstring, then retrieve that documentation while the program is running.
Define the function: Put a string literal immediately after the function definition line so Python treats it as the function's docstring.
Inspect __doc__: Use the function's __doc__ attribute to retrieve the stored documentation.
Use help(): The help() function can display documentation from the function's docstring.
The documentation is available during execution through the function object's __doc__ attribute and through help().
The first call retrieves the docstring through __doc__. The help() call also uses the function's docstring to display documentation.Readability Beyond the Source File
A clear docstring helps someone understand what a function does without reading its implementation. It can also help your future self remember what the function does and what its parameters represent. Because docstrings are available to tools, well-written documentation can appear in IDE tooltips and can be extracted by documentation generators to build reference manuals.
Write documentation that explains the function's purpose clearly enough that a reader does not need to study the implementation first. Treat the docstring as an investment in code clarity: it supports other people who use the function and helps you remember its purpose later.
Mistakes Beginners Make
Treating a comment as if the running program can retrieve it.
Python ignores comments completely; they are not stored or accessible during execution.
Fix:
Use a docstring when the documentation should belong to the function object or be available to help() and documentation tools.Putting the string literal somewhere other than immediately after the function definition line.
A docstring is defined by its placement immediately after the function definition line.
Fix:
Place the documentation string as the first item in the function body.Assuming that a docstring and a comment have the same purpose inside Python.
Both can document code for human readers, but only a docstring is stored as part of the function object and available during execution.
Fix:
Choose a comment for source-only notes and a docstring for documentation that should be retrievable or used by tools.Expecting a docstring to be available without placing it in a function's definition.
The topic here concerns documentation strings stored as part of a function object.
Fix:
Place the string literal immediately after the relevant function definition line.
Practice the Runtime Difference
Consider a function that contains both a hash-prefixed comment and a string literal immediately after its definition line. Identify which text can be retrieved through the function's __doc__ attribute and which text Python ignores.
Hints
- Check whether each piece of text is a comment or a string literal.
- Check the position of the string literal relative to the function definition line.
- Remember that comments are never stored or accessible to the running program.
What do you think happens?
A function has a correctly placed docstring. Which access path can retrieve that documentation during execution?
Reveal answer
Answer: The function's __doc__ attribute
Python stores the docstring as part of the function object, and the documentation can also be displayed with help(). Comments are ignored and are not accessible to the running program.
Key Takeaways
- A comment begins with # and is ignored completely by Python.
- A docstring is a string literal placed immediately after a function definition line.
- Python stores a function's docstring in the function object's __doc__ attribute.
- The __doc__ attribute and help() make function documentation available during program execution.
- Clear docstrings improve readability and support IDE tooltips and documentation generators.
Key Takeaways
- Comments and docstrings both explain code, but Python handles them differently.
- Comments are ignored and cannot be retrieved by the running program.
- A correctly placed docstring becomes part of a function object.
- You can access a function's docstring through __doc__ or help().
- Well-written docstrings improve clarity and support IDEs and documentation tools.