Comments
Comments in Python
Comments in Python or programming in general, are notes added to the source to explain what the code does, it’s purpose and logic. They are ignored by the Python interpreter, because they are only meant to clarify the code.
Types of comments:
- Single-line comments
- Multi-line comments
Single-line comment
Single line comments in python starts with a hash symbol (#) and continue until the end of the line
Example of a single-line comment
Multi-line comment
Multi-line comments are comments that span to multiple lines
Python doesn’t support multi-line comments out of the box so the recommended way is to use multiple #
to write multi-line comments
Example of a multi-line comment
Documentation String
Documentation string in Python are used to clarify the functionality, usage, and purpose of a module, class, or function, helping other developers to easily understand your code without the need of reading through all the details of the implementation.
Docstrings are written using either triple single quotes '''
or triple double quotes """
and they should be the first statement after the definition of the entity they document.
Accessing Documentation String
Sometimes you might want to know what a particular function does, especially some built-in functions so it easier to access their docstring using the following syntax:
Example: using __doc__
attribute
Example: using help()
built-in function
Example:
In this example, we are a documenting a function called display_info