Group comment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ketulp_baroda@yahoo.com

    Group comment

    Hi
    Can I comment a group of statements together i.e
    If I a 4 statements say:
    i=1
    i+=1
    print i
    print i+1

    Instead of commenting each satement iindividually by # is there
    something which allws me to comment these 4 statements in one go as in
    C++ using /*...*/
  • Skip Montanaro

    #2
    Re: Group comment


    ketulp> Can I comment a group of statements together i.e
    ketulp> If I a 4 statements say:
    ketulp> i=1
    ketulp> i+=1
    ketulp> print i
    ketulp> print i+1

    ketulp> Instead of commenting each satement iindividually by # is there
    ketulp> something which allws me to comment these 4 statements in one go
    ketulp> as in C++ using /*...*/

    Many people use triple-quoted strings for this:

    """
    i=1
    i+=1
    print i
    print i+1
    """

    It's not a comment, strictly speaking, however it generally achieves the
    desired results. Even less comment-like is "if False:":

    if False:
    i=1
    i+=1
    print i
    print i+1

    That has the disadvantage that you need to reindent the lines of interest.

    Skip

    Comment

    • Diez B. Roggisch

      #3
      Re: Group comment

      Use (x)emacs and type

      M-x comment-region

      Type

      C-u M-x comment-region

      to uncomment. Of course you can bind that to keys. C-c-< and C-c-> will
      dedent/indent the current selection. Very useful.

      --
      Regards,

      Diez B. Roggisch

      Comment

      • Chema Cortes

        #4
        Re: Group comment

        Skip Montanaro <skip <at> pobox.com> writes:
        [color=blue]
        > Many people use triple-quoted strings for this:
        >
        > """
        > i=1
        > i+=1
        > print i
        > print i+1
        > """
        >
        > It's not a comment, strictly speaking, however it generally achieves the
        > desired results.[/color]

        But as colateral effect it will be interpreted like a doc string. It's better to
        make it as a tuple:

        ("""
        i=1
        i+=1
        print i
        print i+1
        """)

        or, like a hybrid:

        if 0:"""
        i=1
        i+=1
        print i
        print i+1
        """



        Comment

        • Kurt B. Kaiser

          #5
          Re: Group comment

          ketulp_baroda@y ahoo.com writes:
          [color=blue]
          > Can I comment a group of statements together i.e
          > If I a 4 statements say:[/color]

          IDLE has a block comment feature.
          --
          KBK

          Comment

          • ziaran

            #6
            Re: Group comment

            Skip Montanaro wrote:[color=blue]
            > ketulp> Can I comment a group of statements together i.e
            > ketulp> If I a 4 statements say:
            > ketulp> i=1
            > ketulp> i+=1
            > ketulp> print i
            > ketulp> print i+1
            >
            > ketulp> Instead of commenting each satement iindividually by # is there
            > ketulp> something which allws me to comment these 4 statements in one go
            > ketulp> as in C++ using /*...*/
            >
            > Many people use triple-quoted strings for this:
            >
            > """
            > i=1
            > i+=1
            > print i
            > print i+1
            > """
            >
            > It's not a comment, strictly speaking, however it generally achieves the
            > desired results. Even less comment-like is "if False:":
            >
            > if False:
            > i=1
            > i+=1
            > print i
            > print i+1
            >
            > That has the disadvantage that you need to reindent the lines of interest.
            >
            > Skip
            >[/color]

            And keep the """ at the same identation level of the clause!

            Comment

            • David Bolen

              #7
              Re: Group comment

              "Diez B. Roggisch" <nospam-deets@web.de> writes:
              [color=blue]
              > Use (x)emacs and type
              >
              > M-x comment-region
              >
              > Type
              >
              > C-u M-x comment-region
              >
              > to uncomment. Of course you can bind that to keys. C-c-< and C-c-> will
              > dedent/indent the current selection. Very useful.[/color]

              Since presumably you're using python-mode (from your comment about the
              dedent/indent bindings), you may like to know that the commenting
              commands are already bound to keys by default. C-c # to comment,
              C-u C-c # to uncomment.

              -- David

              Comment

              • Bengt Richter

                #8
                Re: Group comment

                On Mon, 1 Mar 2004 08:57:56 -0600, Skip Montanaro <skip@pobox.com > wrote:
                [color=blue]
                >
                > ketulp> Can I comment a group of statements together i.e
                > ketulp> If I a 4 statements say:
                > ketulp> i=1
                > ketulp> i+=1
                > ketulp> print i
                > ketulp> print i+1
                >
                > ketulp> Instead of commenting each satement iindividually by # is there
                > ketulp> something which allws me to comment these 4 statements in one go
                > ketulp> as in C++ using /*...*/
                >
                >Many people use triple-quoted strings for this:
                >
                > """
                > i=1
                > i+=1
                > print i
                > print i+1
                > """[/color]

                Though there's always the corner case where there's already something triple-quoted in
                the segment you want to comment/quote out.

                If we had string-delimited quoting a la mime, we could solve that. E.g.,

                q'arbitrary string'<this is '''quoted''' and """triple is ignored""" ...>'arbitrary string'

                or
                Q'arbitrary string'
                <this is '''quoted''' and """triple is ignored""" ...>
                'arbitrary string'

                Where the upper case Q signals the syntax that the line tail after the delimiter is ignored.
                (thus the second quote above also includes the \n at the end of the quoted < ...> line)


                thus

                Q'XXX'
                i=1
                i+=1
                print i
                print i+1
                'XXX'

                and then no problem to do:

                Q'YYY'
                Q'XXX'
                i=1
                i+=1
                print i
                print i+1
                'XXX'
                'YYY'

                (The single quotes are for readability, not part of the actual quoting delimiters)
                You could deal with the final-escape char problem if you had a no-escapes raw string
                version of this. Maybe signal that with double quoted delimiter strings, e.g.,

                q"+++"this ends in backslash\"+++"

                which would define the same string constant as 'this ends in backslash\\'

                [color=blue]
                >
                >It's not a comment, strictly speaking, however it generally achieves the
                >desired results. Even less comment-like is "if False:":
                >
                > if False:
                > i=1
                > i+=1
                > print i
                > print i+1
                >
                >That has the disadvantage that you need to reindent the lines of interest.[/color]

                The other side of the coin is that it's sometimes useful to write

                if True:
                # .. pasted code that has indentation

                "if True" is also handy to defer processing in interactive mode, e.g.,
                [color=blue][color=green][color=darkred]
                >>> print 'hi'[/color][/color][/color]
                hi[color=blue][color=green][color=darkred]
                >>> print 'ho'[/color][/color][/color]
                ho[color=blue][color=green][color=darkred]
                >>> if True:[/color][/color][/color]
                ... print 'hi'
                ... print 'ho'
                ...
                hi
                ho

                That's my clpy quota for today ;-)

                Regards,
                Bengt Richter

                Comment

                • Josiah Carlson

                  #9
                  Re: Group comment

                  > thus[color=blue]
                  >
                  > Q'XXX'
                  > i=1
                  > i+=1
                  > print i
                  > print i+1
                  > 'XXX'[/color]

                  You may as well suggest /* and */, as they are far more well known, and
                  significantly more acceptable (in my opinion) than your syntax.
                  Considering that /* */ has not already been added to the language, I
                  doubt that an aribirary magical syntax for commenting code would be added.

                  - Josiah

                  Comment

                  • Bengt Richter

                    #10
                    Re: Group comment

                    On Tue, 02 Mar 2004 15:12:03 -0800, Josiah Carlson <jcarlson@nospa m.uci.edu> wrote:
                    [color=blue][color=green]
                    >> thus
                    >>
                    >> Q'XXX'
                    >> i=1
                    >> i+=1
                    >> print i
                    >> print i+1
                    >> 'XXX'[/color]
                    >
                    >You may as well suggest /* and */, as they are far more well known, and
                    >significantl y more acceptable (in my opinion) than your syntax.
                    >Considering that /* */ has not already been added to the language, I
                    >doubt that an aribirary magical syntax for commenting code would be added.
                    >[/color]

                    /*
                    /* you missed the point */
                    */

                    Regards,
                    Bengt Richter

                    Comment

                    • Josiah Carlson

                      #11
                      Re: Group comment

                      >>You may as well suggest /* and */, as they are far more well known, and[color=blue][color=green]
                      >>significant ly more acceptable (in my opinion) than your syntax.
                      >>Considering that /* */ has not already been added to the language, I
                      >>doubt that an aribirary magical syntax for commenting code would be added.
                      >>[/color]
                      >
                      >
                      > /*
                      > /* you missed the point */
                      > */[/color]

                      I understood that nuance, and while your syntax allows nesting of
                      comments, it is still arbitrary and magical. May as well use an "if
                      False:" construct and gain the benefit of visual indentation, regardless
                      of an editor's syntax highlighting capability.

                      - Josiah

                      Comment

                      • Ben Finney

                        #12
                        Re: Group comment

                        On Tue, 02 Mar 2004 17:40:27 -0800, Josiah Carlson wrote:[color=blue]
                        > May as well use an "if False:" construct and gain the benefit of
                        > visual indentation, regardless of an editor's syntax highlighting
                        > capability.[/color]

                        As well as losing any possible ambiguity about whether the code will be
                        executed. I like it.

                        --
                        \ "Truth would quickly cease to become stranger than fiction, |
                        `\ once we got as used to it." -- Henry L. Mencken |
                        _o__) |
                        Ben Finney <http://bignose.squidly .org/>

                        Comment

                        • Bob Ippolito

                          #13
                          Re: Group comment

                          On 2004-03-02 21:30:36 -0500, Ben Finney
                          <bignose-hates-spam@and-benfinney-does-too.id.au> said:
                          [color=blue]
                          > On Tue, 02 Mar 2004 17:40:27 -0800, Josiah Carlson wrote:[color=green]
                          >> May as well use an "if False:" construct and gain the benefit of
                          >> visual indentation, regardless of an editor's syntax highlighting
                          >> capability.[/color]
                          >
                          > As well as losing any possible ambiguity about whether the code will be
                          > executed. I like it.[/color]

                          Some editors will even key in on this, like #if 0 in C.


                          Comment

                          Working...