Skip to content

Explained: Python Comment Usage and Fixing

[

Writing Comments in Python

When writing code in Python, it’s important to make sure that your code can be easily understood by others. Giving variables obvious names, defining explicit functions, and organizing your code are all great ways to do this. Another awesome and easy way to increase the readability of your code is by using comments!

In this tutorial, you’ll cover some of the basics of writing comments in Python. You’ll learn how to write comments that are clean and concise, and when you might not need to write any comments at all.

Why Commenting Your Code Is So Important

Comments are an integral part of any program. They can come in the form of module-level docstrings, or even inline explanations that help shed light on a complex function. But why are comments so important?

When Reading Your Own Code

Consider a scenario where you’re working on a project with a tight deadline. You decide to focus on getting the code to work first and leave the task of adding comments for later. However, as the project progresses and new tasks come up, you forget to go back and add proper comments to your code.

Months later, you need to make changes or fix a bug in that same code, but you’re completely lost because you didn’t comment it properly. You spend hours trying to understand your own code, and the lack of comments makes it even more challenging. This situation can be avoided if you had taken the time to comment your code properly from the beginning.

When Others Are Reading Your Code

Comments are not just beneficial for yourself, but also for others who might need to read and understand your code. Whether it’s a team member, a collaborator, or someone who may inherit your code in the future, comments provide valuable insights into the logic and purpose of your code.

By commenting your code clearly and concisely, you make it easier for others to understand and work with your code. This enhances collaboration and reduces the time it takes for someone else to get up to speed with your codebase.

How to Write Comments in Python

Now that you understand the importance of commenting your code, let’s dive into some basics of writing comments in Python.

Python Commenting Basics

In Python, comments are created using the # symbol. Anything written after the # symbol on a line is considered a comment and is ignored by the interpreter.

# This is a single-line comment

Python Multiline Comments

Python does not have a specific syntax for multiline comments like some other programming languages. However, you can achieve multiline comments by using multiple single-line comments.

# This is a multiline comment
# It is achieved using multiple single-line comments
# These comments are ignored by the interpreter

Python Commenting Shortcuts

While single-line and multiline comments are useful, there are some shortcuts you can use to quickly comment and uncomment blocks of code in most popular integrated development environments (IDEs).

In most IDEs, you can comment or uncomment a block of code by selecting it and pressing a specific keyboard shortcut. For example, in many Python IDEs, you can comment a block of code by selecting it and pressing Ctrl + /. Likewise, you can uncomment a block of code by selecting it and pressing the same keyboard shortcut.

These shortcuts can save you time when you need to add or remove comments from a large portion of code.

Python Commenting Best Practices

Now that you know the basics of writing comments in Python, let’s explore some best practices to follow when adding comments to your code.

When Writing Code for Yourself

Even if you’re writing code that only you will work on, it’s still important to comment your code. This is particularly beneficial when you come back to your own code after a long time or when you’re tackling complex problems.

When writing comments for yourself, focus on providing explanations, clarifications, or reminders that will help you understand the code when you revisit it. Use clear and concise language, and avoid overly technical jargon or unnecessary details.

When Writing Code for Others

When writing code that others will read and work with, you need to consider them as your audience. Your comments should be informative, clear, and provide insights into the purpose and functionality of your code.

Make sure to document any assumptions, dependencies, or external references that are necessary for understanding and working with your code. Also, consider adding inline comments to explain complex logic or to highlight important parts of your code.

Remember to update your comments whenever you make significant changes to the code. Outdated or incorrect comments can mislead others and cause confusion.

Python Commenting Worst Practices

While commenting is crucial, there are some types of comments that you should avoid.

Avoid: W.E.T. Comments

W.E.T. stands for “Write Everything Twice.” This refers to comments that merely repeat what the code already says. These comments add unnecessary clutter and do not provide any additional value.

For example, if you have a line of code that increments a variable named count by 1, a comment saying “Increment count” would be redundant.

Avoid: Smelly Comments

Smelly comments are comments that indicate bad code practices or design choices. Instead of using a comment to explain why the code is not optimal, it’s better to improve the code itself.

If you find yourself writing comments like “This solution is not efficient, but it works for now,” it’s a sign that you need to refactor your code for better performance or readability.

Avoid: Rude Comments

While coding can be frustrating at times, it’s important to maintain a professional and respectful tone in your comments. Avoid using offensive or derogatory language in your comments, even if you’re frustrated with the code or a particular problem.

Remember that your comments are part of the codebase and may be read by others. Keep your comments constructive and focused on improving the code, rather than expressing negative emotions.

How to Practice Commenting

The best way to improve your commenting skills is through practice. Here are a few exercises you can try:

  1. Take an existing codebase and add comments to explain the purpose of each function and the overall logic.
  2. Write a new piece of code and ask someone else to read it. Gather feedback on whether your comments are helpful and understandable.
  3. Review your own code from the past and try to understand it without looking at the comments. Compare your understanding with the original comments and see if they align.

By regularly practicing commenting, you’ll become more proficient at articulating your thoughts and making your code more accessible to yourself and others.

Conclusion

In this tutorial, you’ve learned why commenting your code is important and how to write effective comments in Python. Remember to comment your code for both yourself and others, following best practices and avoiding common pitfalls.

Comments can significantly improve the readability and maintainability of your code, so make it a habit to include clear and concise comments in your Python projects.