Comments vs Docstrings

Understand the difference between comments and docstrings in Python, their purpose, and when to use each.

Loading...
Comments vs Docstrings

Python comments and docstrings both help make code easier to understand, but they serve slightly different purposes.

Let’s see the difference.

Python Comments

Comments are notes you add to your code to explain something to yourself or other developers.

In Python, single-line comments start with #. Python ignores them when executing your code.

For example:

# Calculate the square of a number
def square(n):
    return n ** 2
 
print(square(5))
 
# Output:
# 25

Here, the comment explains what the function does.

Comments are useful for:

  • Explaining logic that might not be obvious.
  • Adding reminders, such as # TODO: Handle invalid input.
  • Explaining why you made a particular decision in your code.

Try to write comments that add useful information instead of simply repeating what the code already says.

Python Docstrings

Docstrings are strings used to describe a module, function, class, or method.

They’re written immediately after its definition and can be accessed at runtime using the __doc__ attribute.

For example:

def square(n):
    """Return the square of a number."""
    return n ** 2
 
print(square.__doc__)
 
# Output:
# Return the square of a number.

Here, the docstring describes the purpose of the square() function.

You can also use help() to view the function’s documentation:

help(square)

Docstrings are useful for:

  • Explaining what a function, class, or module is for.
  • Describing parameters and return values.
  • Helping documentation tools and IDEs display useful information about your code.

Comments vs Docstrings

Here’s a quick comparison:

Feature Comments Docstrings
Syntax Start with # Usually written with """..."""
Main purpose Explain code or reasoning Document a module, function, class, or method
Access at runtime Not available as a docstring attribute Available through __doc__
Common use Clarifications and reminders Descriptions and reference documentation

Can You Use Both Together?

Yes! Comments and docstrings can work together.

For example:

def calculate_total(price, quantity):
    """Return the total cost of the items."""
    # Multiply the price of one item by the quantity
    return price * quantity
 
print(calculate_total(50, 3))
 
# Output:
# 150

The docstring describes what the function does, while the comment explains the calculation inside it.

You don’t need both every time. Use them when they provide helpful information.

When Should You Use Each One?

Here’s a simple way to remember:

  • Use comments to explain how a piece of code works or why you wrote it a particular way.
  • Use docstrings to describe what a function, class, or module does and how to use it.

For example, if a function has several parameters or returns a value that isn’t obvious, a docstring can help someone understand it without reading every line of code.

One important thing to remember: Python doesn’t automatically check whether your comments or docstrings accurately describe your code. It’s up to you to keep them clear and up to date.


👉 Next tutorial: Python PEP 8