Use of comments in Python

It is said that comments tell you why and your code tells you how. When you code, you are telling your computer what to do using just instructions. It is not a human that needs to know the actual reasons why it works but you do have to keep your audience in mind.

Depending on what you are coding, you will find that your use of comments will change so at times they may serve as warnings other times as simple explanations.

In several tutorials or learning resources you will find short comments explaining what does what, as a beginner they will help you understand how the code works and with enough practice you will eventually learn why it works.

Are comments crutches?

This follows the idea that your code cannot be made more readable with more comments. Comments on our programming level are crutches that we will eventually stop relying heavily upon. So it is important to note that your code does not need comments. You should only use them when it cannot be made easier to understand.

Your audience

Your comments will most likely be read. By others, whether that’s a coworker or future you. If you are like me, when you go back to old code (and you remember nothing about it) comments will help you understand it faster.
Regardless of whom is it aimed at, you should avoid repeating things that are obvious in your code. There is an exception to this, when your code looks obvious but it’s not comments are the option. For example, if you’re multiplying a variable by some an arbitrary number, explain why you’re doing so. If you trim something specific from a string before returning, put a comment in explaining why so that the person reading doesn’t have to spend time tracking it down themselves.

How to comment
Another use of comments is to give an overview of what is going on, as opposed to explain each step.
This code is what your comment described, but is commented with higher-level descriptions of what’s going on rather than a line-by-line description of the code.

CC BY 4.0 Mastery07 by 5nbpppkkyj is licensed under a Creative Commons Attribution 4.0 International License.