Is there any way to comment Multiple lines in Python?

7

To comment on a line, we use # .

I would like to know if you can comment on multiple lines in Python 3.

If yes, how should I do it?

    
asked by anonymous 22.01.2018 / 18:35

4 answers

7

As well as placed in the PEP 257 ( strings which are the first statement in a module, class, or function will be considered as strings > special: the docstrings .

  

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

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

It also further states that any other string at any other point in the code will be treated as a comment and therefore ignored by the interpreter.

  

String literals occurring elsewhere in Python code may also act as documentation.

So, very careful! While comments started by # will always be always comments, those starting with quotation marks, 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 by using the dis module:

  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 at Repl.it

Notice that the first thing done is to store the string with multiple lines in __doc__ , because it is the first statement in the module, it will be the docstring . Comment with # was completely ignored when generating opcodes by the interpreter. Notice that the strings within the function were considered within another analysis, displayed there as "code object hello". If you only parse the code inside the function, we will 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 at Repl.it

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

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

    
17.08.2018 / 18:56
4

Here is an example:

'''
Isso e um comentario multilinhas em Python.
'''
    
22.01.2018 / 19:17
1

In the pycharm you can configure in the settings> keymap, it is usually set as default Ctrl + shit +.

follow visual example:

    
17.08.2018 / 19:09
0

Use """ comentário """ to comment on multiple lines.

    
22.01.2018 / 18:42