Lessons · Python · comments (# notes)
Notes to the next reader, who is usually you
A # starts a comment: Python ignores the rest of the line. It is for explaining why, not restating what.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Code is read far more than it is written. A comment that says why a strange line exists saves an hour of archaeology in six months.
How to think about it
Ask: could a reader work this out from the code itself? If yes, no comment. If the reason lives outside the code (a bug you hit, a business rule, an odd input), write that.
Worked example
price = base * 1.2 # VAT is 20% here; rate lives in settings laterThe code says times 1.2; the comment says what 1.2 is.
# retry once: the API drops the first call after idleA why. Nobody could guess this from the retry code alone.
x = x + 1 # add one to xA what. Delete it; the code already says this.
Your turn
Mark the line as a comment so it does not run.
TODO: handle empty input print(total)
Solve one with the tests running
The trap
A commented-out block of old code left 'just in case'. Version control keeps history; the file should keep only what runs.
Practise comments (# notes) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.