Comments are pieces of information present in the middle of code that allows a developer to explain his work to other developers. They make the code more readable and hence easier to debug.
An inline comment is a single line comment and is on the same line as a statement. They are created by putting a ‘#’ symbol before the text.
# This is a single line commment
Note: Although not necessary, according to Python there should be a single space between # symbol and comment text and at least 2 spaces between comment and the statement.
Block comments in Python usually refer to the code following them and are intended to the same level as that code. Each line of block comment starts with a ‘#’ symbol.
# This is a block comment # Each line of a block comment is intended to the same level
GFG GFG GFG GFG GFG
The documentation string is string literal that occurs as the first statement in a module, function, class, or method definition. They explain what can be achieved by the piece of code but should not contain information about the logic behind the code. Docstrings become __doc__ special attribute of that object which makes them accessible as a runtime object attribute. They can be written in two ways:
1. One-line docstring:
"""This is a one-line docstring."""
'''This is one-line docstring.'''
Hello Geek! Help on function my_fun in module __main__: my_fun() Greets the user.
Note that for one-line docstring closing quotes are on the same line as opening quotes.
2. Multi-line docstring:
"""This is a multi-line docstring. The first line of a multi-line doscstring consist of a summary. It is followed by one or more elaborate description. """
Hello Geek! Help on function my_fun in module __main__: my_fun(user) Greets the user Keyword arguments: user -- name of user
The closing quotes must be on a line by themselves whereas opening quotes can be on the same line as the summary line.
Some best practices for writing docstrings:
- Use triple double-quotes for the sake of consistency.
- No extra spaces before or after docstring.
- Unlike comments, it should end with a period.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course