What is the proper way to comment functions in Python?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In Python, the best way to document functions is to use docstrings for behavior and keep inline comments for non-obvious reasoning. Good documentation is not about explaining every line. It is about making intent, inputs, outputs, and failure modes clear to future readers.
Use Docstrings as the Main Contract
For reusable functions, docstrings are the canonical source of usage information. A strong docstring should explain purpose, arguments, return values, and important exceptions.
This format works well in IDE tooltips and generated docs, and it reduces the need to inspect implementation details.
Inline Comments Should Explain Why
Inline comments are useful when the reasoning is not obvious from code itself. Avoid comments that simply restate the line below.
If you find many inline comments describing what code does, prefer refactoring into clearer names and smaller helpers.
Keep Documentation and Type Hints Aligned
Function signatures, type hints, and docstrings should describe the same contract. Drift between them creates hard to diagnose bugs and broken assumptions.
When signature changes, update docstring in the same commit.
Document Public APIs More Than Private Helpers
Not every private helper needs a long docstring. Focus effort where readers need it most:
- Public modules and library entry points.
- Business critical decision logic.
- Functions with non-trivial side effects.
A simple private helper with obvious naming may need no comment at all. Over-documentation can make code noisy and harder to scan.
Add Executable Examples Through Tests
Documentation becomes reliable when examples are backed by tests. If your docstring states behavior, create a matching unit test.
This keeps docs and implementation from drifting apart.
Choose One Docstring Style
Teams generally pick one style such as Google style, NumPy style, or reStructuredText. Any style is fine if it is consistent.
Consistency benefits:
- Predictable reading flow in code reviews.
- Better auto-generated documentation.
- Easier linting and automated checks.
Use one linter configuration and enforce it across repositories to avoid mixed conventions.
Common Pitfalls
- Writing comments for obvious statements instead of clarifying intent.
- Skipping exception behavior in public function docs.
- Letting docstrings become stale after refactors.
- Using vague function names and compensating with long comments.
- Mixing incompatible docstring styles within one codebase.
Summary
- Use docstrings as the primary documentation for Python functions.
- Reserve inline comments for non-obvious design decisions.
- Keep signatures, type hints, tests, and docstrings synchronized.
- Prioritize documentation quality on public and business critical APIs.
- Consistent style and review discipline keep comments trustworthy.
Related reading
- What is the proper way to format a multi-line dict in Python?
- What is the purpose and use of **kwargs?
- What is the purpose and use of kwargs?
- What is the purpose of class methods?
- What is the purpose of meshgrid in NumPy?
- What is the purpose of pip install --user ...?
- What is the purpose of the __repr__ method?
- What is the purpose of the `self` parameter? Why is it needed?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.