Python Comments — Why Deleting One Cost 4% Revenue
A deleted comment hid a tax calculation rule, causing 2 months of wrong discounts.
20+ years shipping production Python across data and backend systems. Drawn from code that ran under real load.
- Comments start with # and are ignored by Python
- Use block comments on their own line, inline comments sparingly
- Python has no multi-line comment syntax — stack # lines
- Triple-quoted strings are not comments; they become docstrings at function/module start
- Only comment WHY, never WHAT — let clean code speak for itself
Imagine you're assembling flat-pack furniture and you scribble 'this screw goes in LAST or the drawer won't open' on the instruction sheet for the next person. That note doesn't change the furniture — it just helps whoever reads the instructions later. Python comments work exactly the same way: they're notes you leave inside your code that Python completely ignores when running the program, but that humans (including future-you) read to understand what's going on.
Every line of code you write today is a mystery you'll need to solve tomorrow. That sounds dramatic, but ask any developer who's opened a file they wrote six months ago — without comments, even your own code can look like a foreign language. Comments are the single cheapest investment you can make to keep your code readable, maintainable, and friendly to every person who touches it after you.
Python runs fast. It processes your instructions line by line, but it skips comments entirely — they exist purely for people, not machines. This solves a real problem: code tells the computer WHAT to do, but it rarely explains WHY a decision was made, what a tricky block is trying to achieve, or what a number like 0.0875 actually represents (is that a tax rate? a discount? who knows). Comments fill that gap.
By the end of this article you'll know exactly how to write single-line comments, how to fake multi-line comments the Pythonic way, how to use docstrings to document functions properly, and — most importantly — when to write a comment versus when to just write cleaner code. You'll also see the mistakes that trip up beginners and the answers that impress interviewers.
Why a Comment Cost 4% Revenue — The Real Cost of Comments in Python
A Python comment is any line or string literal prefixed with # or enclosed in triple quotes (''' or """) that the interpreter ignores. Comments exist solely for human readers — they have zero runtime effect. But that doesn't mean they're free. Every comment is a maintenance liability: it must be updated when the code changes, or it becomes misinformation.
In practice, comments are parsed out during lexical analysis before the AST is built, so they add no overhead to execution. However, they do affect readability, code review time, and — critically — developer trust. A stale comment is worse than no comment: it actively misleads. The Python community follows PEP 8: inline comments are for clarification, block comments explain intent, and docstrings ("""...""") serve as module/class/function documentation accessible via help().
Use comments only when the code cannot be made self-documenting — i.e., when the why is non-obvious but the what is clear. In production systems, over-commenting is a red flag: it often masks unclear logic. The real cost is not runtime but cognitive load and technical debt. One misaligned comment in a payment pipeline once caused a team to misread a rounding rule, costing 4% revenue for three weeks.
round() (banker's rounding). New hire trusted comment, implemented downstream logic expecting truncation — caused 4% revenue loss over 3 weeks.Single-Line Comments — The Workhorse You'll Use Every Day
A single-line comment in Python starts with the hash symbol #. Everything after that # on the same line is ignored by Python. That's the whole rule. There's nothing to install, no special mode to activate — just type # and write your note.
You can put a comment on its own line above the code it describes, which is called a 'block comment'. Or you can put it at the end of a line of code, which is called an 'inline comment'. Both are valid, but block comments are usually easier to read because they don't crowd the code.
Here's the key habit to build early: comment the WHY, not the WHAT. If you write total_price = price + tax and then comment # adds price and tax, that comment adds zero value — anyone can see that. But if you comment # tax is added here because EU law requires it to appear in the final total, now you've explained something the code can't explain itself. That's what comments are for.
Multi-Line Comments — What Python Actually Does (and Doesn't Have)
Here's something that surprises beginners: Python has no official multi-line comment syntax. Languages like Java and JavaScript have / ... / block comments, but Python never added an equivalent. So how do Python developers write longer notes that span multiple lines?
The answer is simple — you just start each line with its own #. Stack as many lines as you need, each prefixed with a #. Python treats each one as a separate single-line comment, but visually they read as a block. This is the officially recommended approach in Python's own style guide (PEP 8).
You'll also see beginners use triple-quoted strings (''' or """) as a workaround for multi-line comments. A string that isn't assigned to a variable and isn't attached to a function is technically just a value Python evaluates and immediately throws away — so it behaves like a comment in practice. However, this is not a true comment, and there's one important exception: when a triple-quoted string appears as the very first statement inside a function, class, or module, it becomes a docstring — a documented description that tools and IDEs can actually read. More on that in the next section.
__doc__. If you use triple quotes as a 'comment' inside a function body, Python still evaluates that string — it's not truly ignored like a # comment is. Stick to # for comments and reserve """ for docstrings.Docstrings — Comments That Your Code Can Actually Read
A docstring is a special triple-quoted string placed as the very first statement in a function, class, or module. It's Python's official way of documenting what a piece of code does — and unlike a regular comment, Python stores the docstring and makes it accessible at runtime via the __doc__ attribute and the built-in function.help()
Think of a regular comment as a sticky note on your desk — helpful to you, invisible to everyone else. A docstring is like a label on a product — it's built into the thing itself and anyone can read it just by inspecting the object.
Docstrings follow a convention: the first line is a short, one-sentence summary of what the function does. If you need more detail, add a blank line, then a longer description, then document the parameters and return value. You don't have to memorise a rigid format right now — just know that the first line matters most and should be a clear, complete sentence.
For beginners, the most important takeaway is this: every function you write should have a docstring. It takes ten seconds, and it pays back tenfold.
# comment and a docstring. The answer that impresses: a comment is stripped from execution entirely and only exists for human readers, while a docstring is stored as the __doc__ attribute of the object and can be read programmatically at runtime by tools like help(), IDEs, and documentation generators like Sphinx.help() is your friend; docstrings make help() actually useful.Best Practices — When to Comment and When to Clean Code
Great developers don't write more comments — they write code that needs fewer comments. The best comment is a well-named variable, a clear function, or a small, focused method. But even the cleanest code can't express business context, historical decisions, or reasoning behind a non-obvious fix.
Here's the practical rule I use on every team: write a comment when the answer to 'Why is this here?' isn't obvious from reading the code alone. If you're explaining a workaround for a third-party bug, add a comment. If you're applying a formula that looks wrong but is correct for the business, add a comment. If you're adding a sleep() to avoid a race condition, for heaven's sake comment that — or it'll be removed in the next 'cleanup' PR.
Also: treat comments like code. They rot. A stale comment is worse than no comment because it actively misleads. During code review, ask the author if each comment is still accurate. Delete comments that describe what the code already says. Update comments when the logic changes.
- Every comment is a maintenance burden. When code changes, comments must change too.
- If a comment is missing, you waste time. If a comment is wrong, you waste even more.
- Invest in readable code first — the comment is the last resort.
- Team rule: during code review, challenge every comment. Is it still accurate? Is it needed?
Style and Conventions — PEP 8 and Team Standards
PEP 8, Python's official style guide, has clear rules for comments. Follow them and your team will read each other's code faster:
- Comments should be complete sentences. Start with a capital letter, end with a period.
- Use a space after
#. It's# commentnot#comment. - Block comments apply to the following code. Indent them to the same level.
- Inline comments use at least two spaces between the code and the
#. - Docstrings use triple double quotes
"""— triple single quotes'''are acceptable but less common.
Most importantly: keep comments relevant. A comment that describes how code works is noise when the code is clear. A comment that describes why the code exists is gold.
Teams that enforce these standards through linters (flake8, pylint) catch comment style violations automatically. It's a small win, but it keeps the codebase consistent.
When Your Own Code Betrays You — The Six-Month Rule
Every senior engineer has this scar. You write something smart at 2 AM, ship it, move on. Six months later, a bug report lands on your desk. You open the file and stare at a function called . What data? What processing? Why does it flip a boolean called process_data()_x? You wrote it, but you might as well be reading assembly. This isn't a memory problem. It's a commenting failure. Your brain optimizes for understanding the code you just wrote. It discards context aggressively. Comments are the lockbox for that context. Write them when you write the code, not after. Future you is not smarter than current you — current you is just closer to the problem. The rule: if you had to think for more than ten seconds to figure out why a line exists, annotate it. Your future self will thank you with fewer debugging sessions and fewer deployment rollbacks.
The Collaborative Codebase — Comments as Contracts
In production, your code is read by ten people for every one person who writes it. Reviewers, testers, on-call engineers, the guy who inherits your module after you quit. Every time you omit a comment, you force each of those people to reverse-engineer your intent. That's a tax on the entire team, compounded every time someone touches that file. Comments are not a courtesy. They are a contract. They tell the next developer: 'I knew this was weird, and I left this note so you don't make the same mistake I almost made.' That contract breaks when you write W.E.T. comments — We Enjoy Typing — that just repeat the code. A comment like # increment counter by 1 next to counter += 1 is noise. It trains readers to ignore comments. Write comments that capture the business rule, the edge case you dodged, or the reason a third-party library is being used in a non-obvious way. That's the signal. Everything else is just duplication.
The Silent Bug: Deleted Sales Due to a Removed Comment
- Never delete a comment unless you fully understand the business context behind it.
- If a comment explains a non-obvious rule, convert it into a test case first.
git log -S 'your_search_term' --all --onelinegit annotate <file> | grep <line>Key takeaways
help(), IDEs, and documentation tools.Common mistakes to avoid
4 patternsCommenting WHAT instead of WHY
counter = counter + 1 # increment counter adds zero value. Anyone can read the code to see what it does. The comment is noise.Using triple-quoted strings as comments inside function bodies
Commenting out dead code and leaving it forever
Stale comments that contradict current logic
Interview Questions on This Topic
What is the difference between a comment and a docstring in Python, and how does Python treat each one at runtime?
__doc__ attribute and can be accessed programmatically via help() or by reading __doc__ directly. Comments document individual lines; docstrings document the purpose and interface of an entire function or class.Frequently Asked Questions
20+ years shipping production Python across data and backend systems. Drawn from code that ran under real load.
That's Python Basics. Mark it forged?
7 min read · try the examples if you haven't