[Tutor] multiline comment in Python

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Sep 4 15:21:46 EDT 2003



On Thu, 4 Sep 2003 tpc at csua.berkeley.edu wrote:

> hello all, I am wondering if there is anything other than triple quotes
> for multiline commenting blocks of code.

Hi tpc,

... that's a novel use of triple quotes that I hadn't thought about!
*grin* It's not really meant for commenting --- it's really meant for
multi-line string literals.



> I am rather disappointed with triple quote commenting because Python
> sometimes gives me a syntax error which means I have to prepend a # in
> front of each line which can get tedious.

Hmmm... what kind of text editor are you using?  In the Emacs text editor,
there is a single command to do block commenting

    C-c #

which is very convenient to work with.  Uncommenting a block is also not
too bad,

    C-u C-c #

But that's just Emacs.  Tell us which text editor you're using; I'm
positive someone here can help you with the correct key contortions.



> I am running Python 2.2.3 and was really hoping for the /** */ multiline
> commenting in C and Java.

I don't think there is one in Python.  It's not too much of a loss,
though, if you are using a good text editor.


But there's a severe problem with multiline commenting in C and Java: it
is not nestable!  For example, let's take some possible C code:

    /** commented out; we don't need it
        printf("This is a test");     /** test to see if standard out
                                          works */
    **/

This just completely breaks because C and Java's lexical analyzer just
doesn't know how to handle it --- the underlying lexer is using a regular
expression and therefore can't keep track of nested comments.

So if you have code with comments in it, trying to use multi-line
commenting on it has the potential to break badly.


The single-line commenting character, on the other hand, has no potential
to suffer from this problem,

    //// commented out; we don't need it
    // System.out.println("This is a test");    // test to see if
    //                                          // standard out works


(But some languages, like OCaml, do have good multi-line commenting
features, so I guess that's not quite as strong a reason as I'd like...
*grin*)



Hope this helps!




More information about the Tutor mailing list