Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between comments in Python, # and """

Tags:

python

Starting to program in Python, I see some scripts with comments using # and """ comments """.

What is the difference between these two ways to comment?

like image 990
altarbza Avatar asked Nov 05 '16 19:11

altarbza


2 Answers

The best thing would be to read PEP 8 -- Style Guide for Python Code, but since it is longish, here is a three-liner:

  • Comments start with # and are not part of the code.
  • String (delimited by """ """) is actually called a docstring and is used on special places for defined purposes (briefly: the first thing in a module or function describing the module or function) and is actually accessible in the code (so it is a part of the program; it is not a comment).
like image 87
Mr. Napik Avatar answered Nov 12 '22 01:11

Mr. Napik


Triple quotes is a way to create a multi-line string and or comment:

"""
Descriptive text here
"""

Without assigning to a variable is a none operation that some versions of Python will completely ignore. PEP 8 suggests when to use block comment/strings, and I personally follow a format like this:

Example Google Style Python Docstrings

like image 3
David Avatar answered Nov 12 '22 01:11

David