tags:

views:

13492

answers:

12

Is there any 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), except that actually makes it show up in various doc tools.

Edit--After reading the answers (and referring to the "duplicate"), I have concluded the correct answer is "No". One person said so, and the rest lectured us about editors. Not a bad thing, but I feel it's important to put the answer at the top.

+8  A: 

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 IDE's 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.

Brian R. Bondy
The op mentioned that they do not want the comments to appear as doc strings.
Ed Swangren
ah, my bad. I gave another suggestion.
Brian R. Bondy
I like this. I'd use if False: though. Not that it matters.
recursive
@recursive: ya changed to if False:
Brian R. Bondy
-1 retracted. That's a clever idea, though it may mean that the comments need comments :)
Ed Swangren
That solution is similar to just commenting out the code, except that you add four spaces instead of # and that you also need to add "if False:" line.
RamyenHead
+13  A: 

No, Python does not have such a mechanism. Block comments are made by prepending each line with a pound sign. For more info, see good ol'-fashioned PEP 8. In any case, almost all Python IDEs support a mechanism to do the block-commenting-with-pound-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.

John Feminella
+21  A: 

The only cure I know for this is a good editor. Sorry.

canen
+0.5 for mentioning that python-aware editors usually help with this need. +0.5 for doing so without starting an editor flame-war by mentioning any editor in particular :-)
Jarret Hardie
Clearly, all Real Python Programmers use ed, where this problem is easily solved with: 12,31s/^/#/
John Fouhy
Tsk. Tsk. You forgot the trademark! "Real Python Programmers" (tm) :-)
Jarret Hardie
+8  A: 

This question was answered previously here: http://stackoverflow.com/questions/397148/why-doesnt-python-have-multline-comments

ChristopheD
A: 

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.

Jaime Soriano
+2  A: 

M-x comment-region, in emacs Python mode.

Joe W.
M-; (comment-dwim) too
RamyenHead
+8  A: 

Hide the triple quotes in a context that won't be mistaken for a docstring, eg:

'''
...statements...
''' and None

or:

if False: '''
...statements...
'''
bobince
A: 

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. Not the best practice though.

Christian Witts
A: 

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.

Anonymous
+2  A: 

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.

Nathan Fellman
+1  A: 

I use NotePad++ on a windows machine, select your code, type CTRL-K. To uncomment you select code and press CTRL-SHFT-K.

Incidentally, NotePad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And its free as in speech and as in beer!!!

dan
A: 

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!^!#!

Jerry