documentation¶
- There are two main approaches to documenting Python code: comments and docstrings.
- comments
- Comments are lines of text within your code that are ignored by the Python interpreter when the program runs.
- They are used to explain the code's functionality, logic, or non-obvious sections.
- Comments are essential for human readers (yourself and other developers) to understand the code's purpose and implementation details.
- Comments - In-line comments
- These are short comments inserted on the same line as the code they explain, using the
#
symbol.
- These are short comments inserted on the same line as the code they explain, using the
In [ ]:
Copied!
# This variable stores the user's name
user_name = "chaitu-ycr"
# This variable stores the user's name
user_name = "chaitu-ycr"
- Comments - Block comments
- These are multi-line comments using triple quotes (
"""
). They are useful for more detailed explanations.
- These are multi-line comments using triple quotes (
In [ ]:
Copied!
"""
This function calculates the area of a rectangle.
Args:
width: The width of the rectangle.
height: The height of the rectangle.
Returns:
The area of the rectangle (width * height).
"""
def calculate_area(width, height):
return width * height
"""
This function calculates the area of a rectangle.
Args:
width: The width of the rectangle.
height: The height of the rectangle.
Returns:
The area of the rectangle (width * height).
"""
def calculate_area(width, height):
return width * height
Best Practices for Comments
- Use comments sparingly and strategically. Well-written code with clear variable names often doesn't require excessive commenting.
- Avoid redundant comments that simply restate the code's functionality. Explain the "why" behind the code, not just the "what."
- Use comments to explain complex logic, non-obvious algorithms, or edge cases.
docstrings
- Docstrings are special string literals placed at the beginning of a module, class, function, or method definition.
- They are accessible using the built-in
help()
function or the object's__doc__
attribute. - Docstrings provide a more structured and comprehensive way to document your code.
Docstring Conventions
- Docstrings typically follow PEP 257 (Python Documentation String Conventions) which outlines a standard format.
In [ ]:
Copied!
"""Short description of the function or object.
Args:
argument_name (type): Description of the argument.
Returns:
return_type: Description of the return value.
Raises:
ExceptionType: Description of the exception that may be raised.
"""
"""Short description of the function or object.
Args:
argument_name (type): Description of the argument.
Returns:
return_type: Description of the return value.
Raises:
ExceptionType: Description of the exception that may be raised.
"""
- Benefits of Docstrings
- Improved Code Readability: Docstrings provide a central location for detailed information about code elements.
- Automatic Documentation Generation: Tools like Sphinx can automatically generate documentation from docstrings.
- Code Reusability: Clear docstrings help others understand how to use your code effectively.