How to comments and documentation use in python?

By Neha, 3 months ago
  • Bookmark
1

How to comment on code? how to use multi line comments? How to use docstrings?

Comment
Documentation
1 Answer
0

Single line, inline and multiline comments :


Comments are used to explain code when the basic code itself isn't clear.

Python ignores comments, and so will not execute code in there, or raise syntax errors for plain English sentences.

Single-line comments begin with the hash character ( # ) and are terminated by the end of line.


Single line comment :

# This is a single line comment in Python


Inline comment :

print("Hello World") # This line prints "Hello World"

Comments spanning multiple lines have """ or ''' on either end. This is the same as a multiline string, but

they can be used as comments:

"""
This type of comment spans multiple lines.
These are mostly used for documentation of functions, classes and modules.
"""


Programmatically accessing docstrings :


Docstrings are - unlike regular comments - stored as an attribute of the function they document, meaning that you

can access them programmatically.


An example function


def func():
	"""This is a function that does nothing at all"""
	return


The docstring can be accessed using the __doc__ attribute:


print(func.__doc__)

This is a function that does nothing at all


help(func)
Help on function func in module __main__ :
func()
This is a function that does nothing at all

Your Answer

Webinars

Know How To Become Data Scientist.

Aug 5th (7:00 PM) 385 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More
BOT
Agent(Online)
We're Online!

Chat now for any query