

Now that you know how to write simple and complex comments while keeping them separate from docstrings, I wanted to discuss when and why to use Python comments in your software development. Why Write Python Comments?Ĭommenting is unquestionably a significant part of any large Python application. I have one recommendation: if you are doubtful, just put a hash mark before the beginning of each comment line to avoid any conflicts with docstrings in your program. Docstrings are used to store documentation in Python modules, functions, classes, and methods.Ĭhoosing between the # character and the """ character when creating multi-line strings can be confusing. This is because using triple double quotes below class, method, or function declaration leads to a docstring declaration. Anything that is written after that until the end of the line counted as a comment. Python comments start with a hash sign # and a white space character. It is always a good idea to include comments in your code when writing new code or updating an old one because you might forget your thought process later on. Sometimes they are also used to help another programmer understand your code. There are many reasons why developers include comments in their code.ĭepending on the length of the program or the purpose of it, comments can be used to make notes for a reader or yourself. Said differently, code instructs computers, while comments instruct humans. These lines are skipped by the compilers and interpreters when the code is executed. What Are Python Comments?Ĭomments can be understood as lines of code that allow a layman to read and understand the code. In this article, I will explain the best way to write Python comments for developers in 2020 (and beyond!). I have seen many bad practices within Python comments over the years.

While professionals might not find it difficult to understand, for someone who is trying to learn Python, including comments in the code can be very helpful. For programmers, it is important that your code is easily understood by outside users.
