Multiline comments in python

Python Multiline Comments Or How To Comment Multiple Lines

Python has several ways to comment multiple lines in Python. One option is to add # at the start of each line. PEP 8 and bigger part of the community prefers to comment out like:

# This is a comment # with multiple lines 
""" This is a comment with multiple lines """ 

Multiline comments in Python can start with »’ and end with »’ . Multiline comment is created simply by placing them inside triple-quoted strings: »’ / «»» and »’ / «»» .

Both examples have valid syntax in Python.

How to comment out multiple lines in Python

In Python there is a special symbol for comments which is # . Some languages like Java have native support for multiline comments.

Python multiline comment would look like to:

# This # is a # multi-line # comment 

This is the default comment for most popular Python IDEs like PyCharm, Sublime, VS code.

Python multiline comments and docstrings

Guido van Rossum (the Python creator, Python BDFL) tweeted once a «pro tip» for Python multiline comments:

According to this tip you can do comments in this way:

"""line1 line2 line3""" '''line1 line2 line3''' 

What is a docstring? The first statement in a class, method, function or module definition which is a string is called a docstring. Example:

def is_palindrome(word): """Check if word is a palindrome.""" str(word) == str(word)[::-1] 

Note 1: Even if a docstring contains only one line, triple quotes should be used because it’s easier to expand it in future.

Читайте также:  Массивы java удалить элементы

Note 2: For one liners it is recommended the quotes to be on the same line as the comment.

Multiline docstrings example

Descriptive multiline docstrings help for understanding and maintaining the code. Here you can find an example for such:

def complex(real=0.0, imag=0.0): """Form a complex number. Keyword arguments: real -- the real part (default 0.0) imag -- the imaginary part (default 0.0) """ if imag == 0.0 and real == 0.0: return complex_zero . 

Many projects and organizations are using this kind of comments when they want to have good documentation.

python-multiline-comments-or-how-to-comment-multiple-lines

The example image below multiline commend and docstring:

Shortcuts to comment multiple lines in Python and most popular IDEs

For commenting several lines in most popular IDEs you can use next shortcuts.

First you need to select the lines and then press:

  • Pycharm — CTRL + / — comment / uncomment
  • Eclipse — CTRL + / — comment / uncomment
  • Sublime — CTRL + / — comment / uncomment
  • Atom — CTRL + / — comment / uncomment
  • IDLE — CTRL + ALT + 3 — comment, CTRL + ALT + 4 — uncomment
  • Notepad++ — CTRL + Q — comment / uncomment
  • vim — CTRL + Q / kbd>CTRL + V — comment / uncomment
  • VS Code — CTRL + / — comment / uncomment

Note: If you like to add a multiline docstring than you can use different combination:

  • Pycharm — Alt + Enter inside the function and select Insert documentation string stub
  • VS Code — Alt + Shift + A — comment / uncomment

PyCharm comment multiple lines

Pycharm comment shortcut

The shortcut to comment multiple lines in Python and PyCharm are:

Pycharm comment out multiple lines

To comment several lines of code in the Pycharm follow next steps:

  • Select the code lines
  • Menu
  • Code
  • Comment with Line Comment
    • Windows or Linux: Ctrl + /
    • Mac OS: Command + /
    # time.sleep(50 / 1000) # 50 ms # time.sleep(5) # 5 secs # time.sleep(60) # 1 min 

    Pycharm uncomment multiple lines

    To uncomment commented lines in PyCharm you can do it by the same steps as commenting:

    • Select the code lines
    • Menu
    • Code
    • Comment with Line Comment
      • Windows or Linux: Ctrl + /
      • Mac OS: Command + /
      time.sleep(50 / 1000) # 50 ms time.sleep(5) # 5 secs time.sleep(60) # 1 min 

      Note: If you try to comment mixed lines code and comments then

      • the first press Ctrl + / will comment all lines (adding the second comment symbol # # in front of the commented lines)
      • the second one Ctrl + / will uncomment all lines (only the first comment sign)
      # time.sleep(50 / 1000) # 50 ms # time.sleep(5) # 5 secs time.sleep(60) # 1 min time.sleep(60 * 60) # 1 hour 
      # time.sleep(50 / 1000) # 50 ms # # time.sleep(5) # 5 secs # time.sleep(60) # 1 min # time.sleep(60 * 60) # 1 hour 

      Python remove all comments from a project with regex finders

      You can delete all Python comments from your Python project by(in Pycharm):

      * doc string with new lines inside 

      Источник

      Python Multiline Comment – How to Comment Out Multiple Lines in Python

      Kolade Chris

      Kolade Chris

      Python Multiline Comment – How to Comment Out Multiple Lines in Python

      Commenting is an integral part of every programming language. With comments, you get a better understanding of your own code, make it more readable, and can help team members understand how it works.

      Comments are ignored by compilers and interpreters, so they don’t run.

      Apart from making your code more readable, comments can also help while you’re debugging – if you have two lines of code, you can comment out one to prevent it from running.

      Just like other programming languages, Python supports comments.

      The problem is that Python doesn’t have a built-in mechanism for multi-line comments.

      So in this article, I won’t just show you how to make single-line comments in Python – I’ll also show you the workaround for making multi-line comments.

      How to Make Single Line Comments in Python

      To make single-line comments in Python, prepend each line with a hash ( # ).

      # print("Hello world") print("Hello campers") 

      As you can see, the commented line wasn’t printed in the output.

      How to Make Multi-line Comments in Python

      Unlike other programming languages such as JavaScript, Java, and C++ which use /*. */ for multi-line comments, there’s no built-in mechanism for multi-line comments in Python.

      To comment out multiple lines in Python, you can prepend each line with a hash ( # ).

      # print("Hello world") # print("Hello universe") # print("Hello everyone") print("Hello campers") 

      With this approach, you’re technically making multiple single-line comments.

      The real workaround for making multi-line comments in Python is by using docstrings.

      If you use a docstring to comment out multiple line of code in Python, that block of code will be ignored, and only the lines outside the docstring will run.

      """ This is a multi-line comment with docstrings print("Hello world") print("Hello universe") print("Hello everyone") """ print("Hello campers") 

      NB: One thing to note is that while using doctsrings for commenting, indentation still matters. If you use 4 spaces (or a tab) for indentation, you will get an indentation error.

      For example, this will work:

      def addNumbers(num1, num2, num3): """ A function that returns the sum of 3 numbers """ return num1 + num2 + num3 print(addNumbers(2, 3, 4)) # Output: 9 
      def addNumbers(num1, num2, num3): """ A function that returns the sum of 3 numbers """ return num1 + num2 + num3 print(addNumbers(2, 3, 4)) 

      So your IDE will throw the error » IndentationError: expected an indented block «.

      Conclusion

      Since there’s no built-in support for multi-line comments in Python, this article demonstrates how you can use docstrings as a workaround.

      Still, you should generally stick to using regular Python comments using a hash ( # ), even if you have to use it for multiple lines. This is because docstrings are meant for documentation, and not for commenting out code.

      If you found this article helpful, consider sharing it with your friends and family.

      Источник

Оцените статью