How to comment out a block of code in Python
Question:
Is there a mechanism to comment out large blocks of Python code?
Right now, the only ways I can see of commenting out code are to either start every line with a #
, or to enclose the code in triple quotes: """
.
The problem with these is that inserting #
before every line is cumbersome and """
makes the string I want to use as a comment show up in generated documentation.
After reading all comments, the answer seems to be “No”.
Answers:
The only way you can do this without triple quotes is to add an:
if False:
And then indent all your code. Note that the code will still need to have proper syntax.
Many Python IDEs can add #
for you on each selected line, and remove them when un-commenting too. Likewise, if you use vi or Emacs you can create a macro to do this for you for a block of code.
Python does not have such a mechanism. Prepend a #
to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-hash-signs automatically for you. For example, in IDLE on my machine, it’s Alt+3 and Alt+4.
Don’t use triple-quotes; as you discovered, this is for documentation strings not block comments, although it has a similar effect. If you’re just commenting things out temporarily, this is fine as a temporary measure.
The only cure I know for this is a good editor. Sorry.
The only mechanism to comment out Python code (understood as code ignored by the interpreter) is the #.
As you say, you can also use string literals, that are not ignored by the interpreter, but can be completely irrelevant for the program execution.
M-x comment-region, in Emacs‘ Python mode.
Hide the triple quotes in a context that won’t be mistaken for a docstring, eg:
'''
...statements...
''' and None
or:
if False: '''
...statements...
'''
Use a nice editor like SciTe, select your code, press Ctrl + Q and done.
If you don’t have an editor that supports block comments you can use a triple quoted string at the start and the end of your code block to ‘effectively’ comment it out. It is not the best practice though.
Triple quotes are OK to me.
You can use ”’ foo ”’ for docstrings and “”” bar “”” for comments or vice-versa to make the code more readable.
At least in VIM you can select the first column of text you want to insert using Block Visual mode (CTRL+V
in non-windows VIMs) and then prepend a #
before each line using this sequence:
I#<esc>
In Block Visual mode I
moves to insert mode with the cursor before the block on its first line. The inserted text is copied before each line in the block.
I use Notepad++ on a Windows machine, select your code, type CTRL-K
. To uncomment you select code and press Ctrl + Shift + K.
Incidentally, Notepad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And it’s free as in speech and as in beer!
In vi:
- Go to top of block and mark it with letter a.
- Go to bottom of block and mark it with letter b
Then do
:'a,'b s!^!#!
comm='''
Junk, or working code
that I need to comment.
'''
You can replace comm
by a variable of your choice that is perhaps shorter, easy to touch-type, and you know does not (and will not) occur in your programs. Examples: xxx
, oo
, null
, nil
.
On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.
Another editor-based solution: text “rectangles” in Emacs.
Highlight the code you want to comment out, then C-x-r-t #
To un-comment the code: highlight, then C-x-r-k
I use this all-day, every day. (Assigned to hot-keys, of course.)
This and powerful regex search/replace is the reason I tolerate Emacs’s other “eccentricities”.
Yes, there is (depending on your editor). In PyDev (and in Aptana Studio with PyDev):
-
Ctrl + 4 – comment selected block
-
Ctrl + 5 – uncomment selected block
In Visual Studio using the Python Tools for Visual Studio, blocks can be commented out by Ctrl+K, Ctrl+C and uncommented by Ctrl+K, Ctrl+U.
In JetBrains PyCharm on Mac use Command + / to comment/uncomment selected block of code. On Windows, use CTRL + /.
Is there a mechanism to comment out large blocks of Python code?
Right now, the only ways I can see of commenting out code are to either start every line with a #
, or to enclose the code in triple quotes: """
.
The problem with these is that inserting #
before every line is cumbersome and """
makes the string I want to use as a comment show up in generated documentation.
After reading all comments, the answer seems to be “No”.
The only way you can do this without triple quotes is to add an:
if False:
And then indent all your code. Note that the code will still need to have proper syntax.
Many Python IDEs can add #
for you on each selected line, and remove them when un-commenting too. Likewise, if you use vi or Emacs you can create a macro to do this for you for a block of code.
Python does not have such a mechanism. Prepend a #
to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-hash-signs automatically for you. For example, in IDLE on my machine, it’s Alt+3 and Alt+4.
Don’t use triple-quotes; as you discovered, this is for documentation strings not block comments, although it has a similar effect. If you’re just commenting things out temporarily, this is fine as a temporary measure.
The only cure I know for this is a good editor. Sorry.
The only mechanism to comment out Python code (understood as code ignored by the interpreter) is the #.
As you say, you can also use string literals, that are not ignored by the interpreter, but can be completely irrelevant for the program execution.
M-x comment-region, in Emacs‘ Python mode.
Hide the triple quotes in a context that won’t be mistaken for a docstring, eg:
'''
...statements...
''' and None
or:
if False: '''
...statements...
'''
Use a nice editor like SciTe, select your code, press Ctrl + Q and done.
If you don’t have an editor that supports block comments you can use a triple quoted string at the start and the end of your code block to ‘effectively’ comment it out. It is not the best practice though.
Triple quotes are OK to me.
You can use ”’ foo ”’ for docstrings and “”” bar “”” for comments or vice-versa to make the code more readable.
At least in VIM you can select the first column of text you want to insert using Block Visual mode (CTRL+V
in non-windows VIMs) and then prepend a #
before each line using this sequence:
I#<esc>
In Block Visual mode I
moves to insert mode with the cursor before the block on its first line. The inserted text is copied before each line in the block.
I use Notepad++ on a Windows machine, select your code, type CTRL-K
. To uncomment you select code and press Ctrl + Shift + K.
Incidentally, Notepad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And it’s free as in speech and as in beer!
In vi:
- Go to top of block and mark it with letter a.
- Go to bottom of block and mark it with letter b
Then do
:'a,'b s!^!#!
comm='''
Junk, or working code
that I need to comment.
'''
You can replace comm
by a variable of your choice that is perhaps shorter, easy to touch-type, and you know does not (and will not) occur in your programs. Examples: xxx
, oo
, null
, nil
.
On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.
Another editor-based solution: text “rectangles” in Emacs.
Highlight the code you want to comment out, then C-x-r-t #
To un-comment the code: highlight, then C-x-r-k
I use this all-day, every day. (Assigned to hot-keys, of course.)
This and powerful regex search/replace is the reason I tolerate Emacs’s other “eccentricities”.
Yes, there is (depending on your editor). In PyDev (and in Aptana Studio with PyDev):
-
Ctrl + 4 – comment selected block
-
Ctrl + 5 – uncomment selected block
In Visual Studio using the Python Tools for Visual Studio, blocks can be commented out by Ctrl+K, Ctrl+C and uncommented by Ctrl+K, Ctrl+U.
In JetBrains PyCharm on Mac use Command + / to comment/uncomment selected block of code. On Windows, use CTRL + /.