473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

comment out more than 1 line at once?

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
Jul 18 '05 #1
15 3502
Riko Wichmann wrote:
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

Jul 18 '05 #2
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

Aaron Bingham wrote:
Riko Wichmann wrote:
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

Jul 18 '05 #3
Riko Wichmann <ri***********@remove-this.desy.de> writes:
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

--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
Jul 18 '05 #4
Riko Wichmann wrote:
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
Jul 18 '05 #5
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


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

Jul 18 '05 #6
On 2004-11-30, Uwe Grauer <ne**@grauer-online.de> wrote:
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
Jul 18 '05 #7
Riko Wichmann <ri***********@remove-this.desy.de> wrote:
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.
Jul 18 '05 #8
Uwe Grauer <ne**@grauer-online.de> writes:
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
--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 18 '05 #9
Riko Wichmann wrote:
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 <gu*************@q-voz.com>
<mailto:gu*************@q-voz.com>
*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.
Jul 18 '05 #10
Gustavo Córdova Avila wrote:
Riko Wichmann wrote:
is there a way in Python to comment out blocks of code without putting
a # in front of each line? Somethings like C's
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
Jul 18 '05 #11

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
Jul 18 '05 #12
Riko Wichmann wrote in news:co************@claire.desy.de 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 18 '05 #13
Rob Williscroft wrote:
Riko Wichmann wrote in news:co************@claire.desy.de 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.

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 <gu*************@q-voz.com>
<mailto:gu*************@q-voz.com>
*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.
Jul 18 '05 #14
Gustavo Córdova Avila wrote in news:mailman.6944.1101838678.5135.python-
li**@python.org 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. :-)

False = True
if False: print "It Seems you are right"
It Seems you are right


:-)

How about:

if 0:
print "unreachable ??"

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 18 '05 #15
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.
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:
import dis
def f(a): ... if False:
... return a
... return a+1
... dis.dis(f) 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) 14 POP_TOP


4 >> 15 LOAD_FAST 0 (a)
18 LOAD_CONST 1 (1)
21 BINARY_ADD
22 RETURN_VALUE def g(a): ... if 0:
... return a
... return a+1
... dis.dis(g)

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
Jul 18 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
13
by: Ray Cassick \(Home\) | last post by:
I have been using XML Comments in VB.NET during my time with VS.NET 2003 thanks to VBXC and NDoc. I just started to upgrade one of my projects into VB.NET 2005 and am seeing some odd errors...
8
by: hobbes_7_8 | last post by:
Hi everybody! This is basically a pre-processor doubt. I have this very simple define: #ifdef NDEBUG #define QTRACE // #else #define QTRACE qDebug() #endif
6
by: cdrsir | last post by:
we can use 1) // my comments a 2) /* my comments b */ when we want to add some comments. I know some compilers just support the 2nd syntax, but normally both of these 2 syntaxs are supported...
16
by: Christopher | last post by:
Hello all, I've got a question of comment logic. I'm trying to determine if a given line of C/C++/C#/Java is a comment or not. Can you please comment on my logic. I need to define some...
3
by: Martin P. Hellwig | last post by:
Hi all, I've been toying with python for about two years now. Not every day, just when I encounter something in my job (sysadmin) repetitively dull. The amazing thing is that like any other...
29
by: dbhbarton | last post by:
Had a thought that's grown on me. No idea if it's original or not- too inexperienced in programming- but I guess there's no harm floating it out there. Python wins big on readability, and...
14
by: Siv | last post by:
Hi, Just busily coding away and removed a procedure from my code and all of a sudden an error came up miles away from the location of the piece of code I removed and it relates to the XML...
39
by: polas | last post by:
Afternoon all. I was just wondering about this point - I have (generally) used // for commenting a single line in C, but from looking at code other people have written it seems many use /* */...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.