comment out more than 1 line at once?

R

Riko Wichmann

Dear all,

is there a way in Python to comment out blocks of code without putting a
# in front of each line? Somethings like C's

/*
block of code here is commented out
*/

Thanks,

Riko
 
A

Aaron Bingham

Riko said:
Dear all,

is there a way in Python to comment out blocks of code without putting
a # in front of each line? Somethings like C's

/*
block of code here is commented out
*/

No. Why do you want this? Any good programmer's editor will have a
comment/uncomment feature to make it easy to comment out whole blocks of
code at a time using the hash character ('#').

Aaron
 
R

Riko Wichmann

I'm using emacs (with python-mode) to do most of my editing. also tried
IDLE from python already, which is nice but an old dog doesn't like to
learn new tricks :)

Maybe I just don't know to comment out whole blocks using editor
commands. Probably the trick Marc mentions in the following post will do
the job.

I use it to test pieces of a modules which usually is imported from
another. For testing different parts, I usually have different __main__
sections in the file ....

Cheers,

Riko
 
B

Bernhard Herzog

Riko Wichmann said:
I'm using emacs (with python-mode) to do most of my editing. [...]
Maybe I just don't know to comment out whole blocks using editor
commands.

comment-dwim (usually bound to M-; ) comments out the region if it's
active, or, if the region is already commented, uncomments it.

Bernhard
 
U

Uwe Grauer

Riko said:
I'm using emacs (with python-mode) to do most of my editing. also tried
IDLE from python already, which is nice but an old dog doesn't like to
learn new tricks :)

Maybe I just don't know to comment out whole blocks using editor
commands. Probably the trick Marc mentions in the following post will do
the job.

I use it to test pieces of a modules which usually is imported from
another. For testing different parts, I usually have different __main__
sections in the file ....

Cheers,

Riko

Riko,

if you are using emacs, try C-c # to comment out a block of code.
Unfortunally Uncomment is not bound to a key by default.
Do a key-binding for it by yourself.

Uwe
 
R

Riko Wichmann

Ha! have just discover C-c # myself (sometimes it helps to actually look
at the emacs menus, even if you are used to using the key-bindings :)

Better yet works "Meta-;" as Bernhard suggested, because it comments and
un-comments marked line!

Thanks for all the helpful tips!

- Riko
 
D

Dave Cook

if you are using emacs, try C-c # to comment out a block of code.
Unfortunally Uncomment is not bound to a key by default.

Uncomment is C-u C-c #, at least in xemacs. Rectangle delete sometimes
works in a pinch (C-x r k, once you've marked the rectangle.)

Dave Cook
 
R

Roy Smith

Riko Wichmann said:
I'm using emacs (with python-mode) to do most of my editing. also tried
IDLE from python already, which is nice but an old dog doesn't like to
learn new tricks :)

Emacs is great for stuff like this. There's probably other ways to do
it, but what I would do is a quick keyboard macro. Go to the beginning
of the first line and type:

C-X ( # C-A C-N C-X )

That defines a macro which inserts a # then moves to the beginning of
the next line. Then you need to execute that once for each additional
line you want to comment out. If there's 17 additional lines, you would
do:

C-U 1 7 C-X E

Hmmm. I just did help-appropos on "comment" and of course I found the
better way I suspected must exist :) You can just use M-X
comment-region or M-X py-comment-region.

Still, learning how to do keyboard macros will really improve your
productivity in emacs. There's often a better way to do something, but
for simple repetitive tasks, you can often write and execute a keyboard
macro faster than you can look up the better way.
 
J

Jerry Sievers

Uwe Grauer said:
Riko,

if you are using emacs, try C-c # to comment out a block of code.
Unfortunally Uncomment is not bound to a key by default.
Do a key-binding for it by yourself.

In Emacs, you can have the comment-region command do uncomment by
typing C-u first;

C-c # comment region
C-u C-c # uncomment region

From the Emacs online help for plain comment-region'. you are
probably invoking py-comment-region which is just a python tailored
version;

Comment or uncomment each line in the region. With just C-u
prefix arg, uncomment each line in region. Numeric prefix arg
ARG means use ARG comment characters. If ARG is negative,
delete that many comment characters instead. Comments are
terminated on each line, even for syntax in which newline does
not end the comment. Blank lines do not get comments.

....Curiosly, I tried the numeric prefix arg to see it use 2 or more
comment chars or delete as many and I get 2x the number of the prefix
arg.

C-u 2 C-c # and I get;

####def foo():

HTH
 
?

=?ISO-8859-1?Q?Gustavo_C=F3rdova_Avila?=

Riko said:
Dear all,

is there a way in Python to comment out blocks of code without putting
a # in front of each line? Somethings like C's

/*
block of code here is commented out
*/

Thanks,

Riko

I haven't seen the "other" way to block-comment multiple
lines of code: triple-quoted strings.

Strings which are unassigned in a source file are dropped
by the compiler, so they don't make it to the execution
stage; they're nice that way.
--
Gustavo Córdova Avila <[email protected]>
<mailto:[email protected]>
*Tel:* +52 (81) 8130-1919 ext. 127
Integraciones del Norte, S.A. de C.V.
Padua #6047, Colonia Satélite Acueducto
Monterrey, Nuevo León, México.
 
P

Peter Hansen

Gustavo said:
I haven't seen the "other" way to block-comment multiple
lines of code: triple-quoted strings.

Building on this thought: it's very effective to write all
your "real" triple-quoted strings (usually they're the doc-comments,
but not always) using one type of quotation mark, thus reserving
the other type for use as temporary "commenting" as Riko asks:

"""
def func(foo, bar):
'''this function spams a foo with a bar'''
pass
"""

If you're consistent about it, it's at least as effective as
multi-line comment in other languages for this purpose.
Strings which are unassigned in a source file are dropped
by the compiler, so they don't make it to the execution
stage; they're nice that way.

Not that it matters, but note that the first "unassigned"
string in a file (or class, or function, etc) will not be
dropped but will be available as the __doc__ attribute of
the module, class, function, or whatever.

I guess this could matter if someone was trying to ship
only .pyc files, thought that some critical and/or embarrassing
code was actually eliminated, but found out that it was
all nicely visible in the doc-comment for the module in
question...

-Peter
 
S

Skip Montanaro

Riko> I'm using emacs (with python-mode) to do most of my editing.

C-c # is bound to py-comment-region in python-mode.

Skip
 
R

Rob Williscroft

Riko Wichmann wrote in in
comp.lang.python:
Dear all,

is there a way in Python to comment out blocks of code without putting a
# in front of each line? Somethings like C's

/*
block of code here is commented out
*/

if False:

(indented) block of code here is commented out

I've no idea how efficient it is compared to triple-quoted strings
or line by line comments. But (at least with the editors I use) it
does retain the syntax highlighting, which I quite like YMMV.

BTW for C and C++:

#if 0

block of code here is commented out
/* with embeded comments ! */

#endif

Rob.
 
?

=?ISO-8859-1?Q?Gustavo_C=F3rdova_Avila?=

Rob said:
Riko Wichmann wrote in in
comp.lang.python:

if False:

(indented) block of code here is commented out

I've no idea how efficient it is compared to triple-quoted strings
or line by line comments. But (at least with the editors I use) it
does retain the syntax highlighting, which I quite like YMMV.

BTW for C and C++:

#if 0

block of code here is commented out
/* with embeded comments ! */

#endif

Rob.
Actually, it's infinitly [sp?] more defficient
(contrary of efficient?) than triple-quoted strings
or line-by-line comments, because those two never
make it to execution stage, because they're dropped
by the compiler. :)
--
Gustavo Córdova Avila <[email protected]>
<mailto:[email protected]>
*Tel:* +52 (81) 8130-1919 ext. 127
Integraciones del Norte, S.A. de C.V.
Padua #6047, Colonia Satélite Acueducto
Monterrey, Nuevo León, México.
 
R

Rob Williscroft

Gustavo Córdova Avila wrote in (e-mail address removed) in comp.lang.python:
Actually, it's infinitly [sp?] more defficient
(contrary of efficient?) than triple-quoted strings
or line-by-line comments, because those two never
make it to execution stage, because they're dropped
by the compiler. :)
print "It Seems you are right"


It Seems you are right
:)

How about:

if 0:
print "unreachable ??"

Rob.
 
S

Skip Montanaro

Gustavo> Actually, it's infinitly [sp?] more defficient (contrary of
Gustavo> efficient?) than triple-quoted strings or line-by-line
Gustavo> comments, because those two never make it to execution stage,
Gustavo> because they're dropped by the compiler. :)

If you use 'if 0:' the 2.4 byte code compiler also deletes it:
... if False:
... return a
... return a+1
... 2 0 LOAD_GLOBAL 0 (False)
3 JUMP_IF_FALSE 8 (to 14)
6 POP_TOP

3 7 LOAD_FAST 0 (a)
10 RETURN_VALUE
11 JUMP_FORWARD 1 (to 15)
4 >> 15 LOAD_FAST 0 (a)
18 LOAD_CONST 1 (1)
21 BINARY_ADD
22 RETURN_VALUE ... if 0:
... return a
... return a+1
... 4 0 LOAD_FAST 0 (a)
3 LOAD_CONST 1 (1)
6 BINARY_ADD
7 RETURN_VALUE

The 'if False:' case isn't optimized because you can (today, anyway)
redefine or override False.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top