Comments in Python : The Ultimate Guide to Writing Clean and Maintainable Code

Comments in Python : The Ultimate Guide to Writing Clean and Maintainable Code

A comprehensive guide on Comments in Python Programming

In this article, we'll learn about comments in Python, types, uses and best practices for writing comments.

In the previous article titled: Python Variables Demystified: Discover the Key to Efficient and Dynamic Python Programming, we did a comprehensive guide on Python Variables.

Comments

A simple text or hints expressing a thought or suggestion in the context of its usage. It could serve as an assistance to the author of the comments on a note or the readers in the future. A typical example is when someone is asked to comment on something to provide clarity on a subject matter.

Comments in Python

Comments in Python are text or brief hints on lines of code written to provide a guide or clearance within the context of its use and exclusively reserved to inform programmers which is ignored by Python's interpreter. Comments in Python begin with #.

#demo of comments in Python
print('A python developer....')

Uses of Comments in Python Programming

It's paramount to define the use of comments to avoid misuse or inappropriate additions that will result in messy and poor codes.

  • It is used to explain the code (line of code, function or class)

  • It makes the maintenance of code easy.

  • Improves readability of code.

  • Makes code inheritance smooth for another developer.

  • Additional resources can be mentioned. Probably there's likely to be a use of a module

Single -line comment

A comment in one line is a single comment

#single-line comment
print('A python developer....')

Multi-line comments

The # or triple quote """ """" can be used for multi-line comments. Using the # for multi-line comments is better. If the triple quote gets assigned to a variable Python will interpret and allocate memory space for it.

Using the # symbole for multi-line statement:

#function for checking student dept
#This check is based on x rule of dept validation
#refer to dept_validation module for more details
def dept(stu_dept):
    if stu_dept != '':
        print('student dept is:' + stu_dept)
    else:
        print('student default dept is computer science')


dept("software engineering")

Using the triple quotes """ """ for comment:

"""
    function for checking student dept
    This check is based on x rule of dept validation
    refer to dept_validation module for more details
"""
def dept(stu_dept):
    if stu_dept != '':
        print('student dept is:' + stu_dept)
    else:
        print('student default dept is computer science')


dept("software engineering")

Same output and none of the comments gets interpreted by Python.

Best practices for Commenting

These practices are useful once commenting is involved in writing codes. In most cases, a well-written literally may not need comments except for large codebases.

  • Make it precise. Keep it simple.

  • Ensure it's relevant.

#this line prints the user name
print("Alemsbaja")

The line of comment above isn't necessary because the code is pretty simple to understand. In a project with newbies/beginners, this might be helpful for them but such a codebase should not be shipped for production purposes.

  • Avoid repetition of comments

  • Avoid writing comments that are not tailored for a specific purpose or fit into the context in which it was written

Conclusion

Comments in Python make code readability easy for the developer and other developers who will be involved in working on the project. The # symbol before every statement or line of code essentially informs the interpreter to ignore the line and treat it as a comment. Commenting helps ease the Inheritance of the codebase. Comments duplication, writing comments that are not useful, and comments on every line of code are some of the reasons for the contention as to why comments aren't necessary for coding but when it becomes important to comment ensure to apply best practices.

If you're looking forward to developing applications in Python using the OOP (Object-oriented programming), the major concepts are simplified on my blog here.

Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!