Comments are used to make notes in source code about what a piece of code does or to not include executable lines of code when the application is run.
Comments are created in Python by adding a hash tag symbol in front of the code to exclude. Example:
# This is a comment.
There is not a formal way to comment multiple lines of code. The best way is to use the hash tag on each line to be commented. Example:
# This is a
# multi line comment.
There is a hack though, which is using triple quotes around the block of code to comment. Example:
“””
This is a
multi-line comment.
“””
This hack should not be used after a function signature, class definition or at the beginning of a module. In these locations, the lines are included in the bytecode and turn into a docstring. Docstrings are similar to the javadoc or jsdoc comments added before a class or function in Java or JavaScript. Docstrings are not stripped out during parsing and remain during runtime where they can be retrieved for usage, such as in a help system or as metadata. Hash tag comments are not included in the bytecode, no matter where they are used.
It’s best to avoid the multi-line comment hack unless you’re just using it during development for a quick test and will take them out when done.
Leave a Reply
You must be logged in to post a comment.