Is there any way to comment on multiple lines in Python?

Asked

Viewed 41,980 times

9

To comment on a line, we use the #.

I wonder if it is possible to comment multiple lines in Python 3.

If so, how should I?

  • 2

    If you’re using Sublime Text and want to comment multiple lines, just select and press CTRL + /

4 answers

12

Just as placed in the PEP 257 (That’s PEP), strings which are the first instruction in a module, class or function will be considered as strings special: the docstrings.

A docstring is a literal string that occurs as the first statement in a module, Function, class, or method Definition.

As docstrings are used by Python itself for documentation and are accessible through its objects by the field __doc__.

Further on it also states that any other string at any other point in the code will be treated as comment and therefore ignored by the interpreter.

String literals occurring Elsewhere in Python code may also Act as Documentation.

So, very carefully! While comments started by # evening at all times comments, those initiated by quotes, whether single or double, may not be comments.

""" Isto NÃO É um comentário.

String com múltiplas linhas que será docstring do módulo.
"""

# Isso É um comentário

def hello():
    'Isso NÃO É um comentário'
    print('hello')  # Isso É um comentário
    'Isso É um comentário'

You can confirm this using the module dis:

  4           0 LOAD_CONST               0 (' Isto NÃO É um comentário.\n\nString com múltiplas linhas que será docstring do módulo.\n')
              2 STORE_NAME               0 (__doc__)

  8           4 LOAD_CONST               1 (<code object hello at 0x7f340fc28270, file "<dis>", line 8>)
              6 LOAD_CONST               2 ('hello')
              8 MAKE_FUNCTION            0
             10 STORE_NAME               1 (hello)
             12 LOAD_CONST               3 (None)
             14 RETURN_VALUE
None

See working on Repl.it

Realize that the first thing done is to store the string with multiple lines in __doc__, because, as is the first instruction of the module, will be the docstring of this. The comment with # was completely ignored when generating opcodes by the interpreter. Note that the strings within the function were considered within another analysis, displayed there as "code Object hello". If you just look at the internal code for the function, we’ll have:

  2           0 LOAD_CONST               0 ('Isso NÃO É um comentário')
              2 STORE_NAME               0 (__doc__)

  3           4 LOAD_NAME                1 (print)
              6 LOAD_CONST               1 ('hello')
              8 CALL_FUNCTION            1
             10 POP_TOP

  4          12 LOAD_CONST               2 (None)
             14 RETURN_VALUE
None

See working on Repl.it

Where, again the first string will not be a comment, as it will be interpreted as docstring of the function, while the others were ignored.

In conclusion, you can use any variation of string Python as a commentary, as long as it is not interpreted as a docstring.

7

Below is an example:

'''
Isso e um comentario multilinhas em Python.
'''
  • 5

    Just a detail to enlighten the readers. This is actually a multiline string, which by chance will not be used for anything of the form that was declared, because it was not used in function nor assigned to some variable. This post helps to understand: https://answall.com/questions/80551/70

  • More than that, the first occurrence in the module is treated as docstring. More details in https://www.python.org/dev/peps/pep-0257/

1

In pycharm you can configure in Settings>keymap, usually comes configured as default Ctrl+shit+.

follows visual example: inserir a descrição da imagem aqui

1

Utilize """ comentário """ to comment in multiple lines.

  • 2

    Worth the comment I made in the other reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.