Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple line comment in Python [duplicate]

Is there a way to give multiple line comments in Python?

Like it is in case of C/C++ : /*comment*/. Or does it have to be marked "#" in front of every line?

like image 324
Gaurav Rai Avatar asked Jan 21 '14 08:01

Gaurav Rai


People also ask

How do you comment multiple lines at once?

To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ ) To uncomment multiple code lines right-click and select Source > Remove Block Comment. ( CTRL+SHIFT+\ )

Can comments also span multiple lines?

On the other hand, multi-line comments can span many lines or be placed within a code statement and only the content between the comment delimiters will be treated as the comment and ignored during compilation.


1 Answers

Try this

'''
This is a multiline
comment. I can type here whatever I want.
'''

Python does have a multiline string/comment syntax in the sense that unless used as docstrings, multiline strings generate no bytecode -- just like #-prepended comments. In effect, it acts exactly like a comment.

On the other hand, if you say this behavior must be documented in the official docs to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification.

In any case your editor should also be able to easily comment-out a selected region (by placing a # in front of each line individually). If not, switch to an editor that does.

Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived.

Not only should the editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful.

like image 99
user3218338 Avatar answered Oct 13 '22 23:10

user3218338