Create multiline comments in Python?

Veröffentlicht am: 23 November 2016
auf dem Kanal: The American Developer
610
3

PLEASE COMMENT, SUBSCRIBE, AND SHARE!!!
Way to create multiline comments in Python?

Inspired by:
http://stackoverflow.com/questions/76...

Best Answer Section:
"

up vote
847
down vote
accepted
You can use triple-quoted strings. When they're not a docstring (first thing in a class/function/module), they are ignored.

'''
This is a multiline
comment.
'''
Guido van Rossum (creator of Python) tweeted this as a "pro tip".

However, Python's style guide, PEP8, favors using consecutive single-line comments, and this is also what you'll find in many projects. Editors usually have a shortcut to do this easily.

shareimprove this answer
edited Jun 30 at 14:27

Martijn Pieters♦
511k7513581508
answered Oct 8 '11 at 12:58

Petr Viktorin
26.4k24660
7
Hm. I put a huge multiline string in a python script test.py just to see. When I do import test, a test.pyc file is generated. Unfortunately, the pyc file is huge and contains the entire string as plain text. Am I misunderstanding something, or is this tweet incorrect? – unutbu Oct 8 '11 at 13:18
16
@unutbu, if it was the only thing in the file, it was a docstring. Put some code before it and it'll disappear from the pyc. I edited the answer and put „module“ to the list of things that have docstrings. – Petr Viktorin Oct 8 '11 at 13:21
18
I don't like multiline string as comments. Syntax highlighting marks them as strings, not as comments. I like to use a decent editor that automatically deals with commenting out regions and wrapping multiline comments while I type. Of course, it's a matter of taste. – Sven Marnach Oct 8 '11 at 13:31
37
As a convention I find it helpful to use """ for docstrings and ''' for block comments. In this manner you can wrap ''' around your usual docstrings without conflict. – Roshambo Dec 18 '12 at 20:03
3
While you can use multi-line strings as multi-line comments, I'm surprised that none of these answers refer to the PEP 8 subsection that specifically recommends constructing multi-line comments from consecutive single-line comments, with blank # lines to distinguish paragraphs. – Air May 21 '14 at 19:32
"


Auf dieser Seite können Sie das Online-Video Create multiline comments in Python? mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer The American Developer 23 November 2016 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 610 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!