What is the proper way to comment functions in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.