why no block comments in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    why no block comments in Python?

    I'm still tyring to figure out what "Pythonic" means, and I have a
    feeling the answer to my question may fall into that category. Are block
    comments somehow unpythonic?
  • Fredrik Lundh

    #2
    Re: why no block comments in Python?

    John Salerno wrote:
    [color=blue]
    > I'm still tyring to figure out what "Pythonic" means, and I have a
    > feeling the answer to my question may fall into that category. Are block
    > comments somehow unpythonic?[/color]

    only in the sense that python don't have them.

    but they're pretty pointless, if you have a modern editor.

    (and if you don't, you can quickly comment out regions by putting them
    inside a triple-quoted string.)

    </F>



    Comment

    • Roy Smith

      #3
      Re: why no block comments in Python?

      Fredrik Lundh <fredrik@python ware.com> wrote:[color=blue]
      > you can quickly comment out regions by putting them
      > inside a triple-quoted string.)[/color]

      Except that triple-quotes don't nest.

      I do agree, however, with the idea that any decent editor should be
      able to comment out a block of code faster than I can type this
      sentence.

      Comment

      • Warby

        #4
        Re: why no block comments in Python?

        It's clear that if you have a modern editor, block comments are
        unnecessary because it is trivial to add a # to the start of each line
        of a block, but that doesn't really answer your question. It explains
        why you might not always need block comments but doesn't explain why
        you shouldn't use them (especially in a primitive editor).

        The danger with block comments is that there is no way to tell that the
        code you're looking at has been commented out unless you can see the
        start or end of the comment block. If you have a modern editor, it
        probably changes the color of all commented out code to eliminate
        confusion. But if you have a primitive editor it does not. Also, even
        people who use modern editors sometimes browse source code using a
        plain text viewer (less/more).

        Eliminating block comments eliminates uncertainty. :)

        Comment

        • Warby

          #5
          Re: why no block comments in Python?

          ....and I forgot to mention that the output of grep and diff is far more
          understandable in the absence of block comments!

          Comment

          • John Salerno

            #6
            Re: why no block comments in Python?

            Warby wrote:
            [color=blue]
            > The danger with block comments is that there is no way to tell that the
            > code you're looking at has been commented out unless you can see the
            > start or end of the comment block. If you have a modern editor, it
            > probably changes the color of all commented out code to eliminate
            > confusion. But if you have a primitive editor it does not.[/color]

            That makes sense. If you have a modern editor, you don't need blocks. If
            you don't have one, blocks don't help. :)

            Comment

            • Fredrik Lundh

              #7
              Re: why no block comments in Python?

              Roy Smith wrote:
              [color=blue][color=green]
              > > you can quickly comment out regions by putting them
              > > inside a triple-quoted string.)[/color]
              >
              > Except that triple-quotes don't nest.[/color]

              ''' """...excep t when they do""" '''

              </F>



              Comment

              • Roy Smith

                #8
                Re: why no block comments in Python?

                Warby <Mark.Warburton @gmail.com> wrote:[color=blue]
                >Eliminating block comments eliminates uncertainty. :)[/color]

                An even better way to eliminate uncertainty is to eliminate the code.
                Commenting out is fine for a quick test during development. Once the
                code is committed, the dead code should be eliminated completely.

                Comment

                • Terry Hancock

                  #9
                  Re: why no block comments in Python?

                  On Wednesday 08 March 2006 12:42 pm, Warby wrote:[color=blue]
                  > The danger with block comments is that there is no way to tell that the
                  > code you're looking at has been commented out unless you can see the
                  > start or end of the comment block. If you have a modern editor, it
                  > probably changes the color of all commented out code to eliminate
                  > confusion. But if you have a primitive editor it does not. Also, even
                  > people who use modern editors sometimes browse source code using a
                  > plain text viewer (less/more).[/color]

                  No doubt some Emacs zealot will say something snarky at this point, ;-)
                  but it's also true that Vi (or gvim anyway) will occasionally get
                  confused by very long block comments or triple-quoted strings,
                  causing the syntax-color to get out of synch.

                  I recently started running into this problem when I started using
                  doctest tests. There's probably a smarter way to do this, but I
                  was putting several of them in a module docstring, and it gets to
                  be a 100+ lines or so of doctest plus explanations.

                  I'm thinking this might be a use-case for the new support for
                  doctests in a separate file. Or maybe I just need to see if I
                  can move the tests into individual object docstrings.

                  --
                  Terry Hancock ( hancock at anansispacework s.com )
                  Anansi Spaceworks http://www.anansispaceworks.com

                  Comment

                  • Paddy

                    #10
                    Re: why no block comments in Python?

                    I have found that some editors colourize text based on parsing a
                    section of text around what is visible. Long, multi-line comments or
                    strings might not then get colored correctly.

                    Personally, I do use block comments in other languages but maybe they
                    should not exist in finished code for reasons already given by others,
                    readabiity!

                    Cheers, Paddy.

                    Comment

                    • Benji York

                      #11
                      Re: why no block comments in Python?

                      Terry Hancock wrote:[color=blue]
                      > I'm thinking this might be a use-case for the new support for
                      > doctests in a separate file.[/color]

                      Having doctests in their own file is (IMHO) a majorly under appreciated
                      feature of doctest. The ability to do either nice user (as in
                      developer) docs with known good examples or well documented
                      not-meant-for-documentation unit/functional/integration tests is terrific.
                      --
                      Benji York

                      Comment

                      • msoulier

                        #12
                        Re: why no block comments in Python?

                        > (and if you don't, you can quickly comment out regions by putting them[color=blue]
                        > inside a triple-quoted string.)[/color]

                        Although that will use up memory, as opposed to a comment.

                        Still, it's simple enough in an editor like Vim or Emacs to highlight a
                        region, and define a macro to add/remove #s. Any Python IDE should
                        certainly have this capability.

                        Mike

                        Comment

                        • Roy Smith

                          #13
                          Re: why no block comments in Python?

                          msoulier <msoulier@gmail .com> wrote:[color=blue][color=green]
                          >> (and if you don't, you can quickly comment out regions by putting them
                          >> inside a triple-quoted string.)[/color]
                          >
                          >Although that will use up memory, as opposed to a comment.[/color]

                          I can't imagine a realistic scenario where the amount of memory wasted
                          by triple-quoting out code could possibly be significant.

                          I'll also repeat what I said before -- good software engineering
                          practice demands that you remove dead code completely. Commenting
                          something out for a quick test during development is OK, but once it
                          reaches the production stage, get rid of it. It'll still live in your
                          revision control system.

                          Comment

                          • Peter Otten

                            #14
                            Re: why no block comments in Python?

                            msoulier wrote:
                            [color=blue][color=green]
                            >> (and if you don't, you can quickly comment out regions by putting them
                            >> inside a triple-quoted string.)[/color]
                            >
                            > Although that will use up memory, as opposed to a comment.[/color]

                            Doesn't seem so:
                            [color=blue][color=green][color=darkred]
                            >>> def f():[/color][/color][/color]
                            .... "docstring"
                            .... "another string"
                            .... a = 42
                            .... "yet another string"
                            ....[color=blue][color=green][color=darkred]
                            >>> f.func_code.co_ consts[/color][/color][/color]
                            ('docstring', 42, None)[color=blue][color=green][color=darkred]
                            >>>[/color][/color][/color]

                            Peter

                            Comment

                            • Terry Hancock

                              #15
                              Re: why no block comments in Python?

                              On 9 Mar 2006 07:21:00 -0800
                              "msoulier" <msoulier@gmail .com> wrote:[color=blue][color=green]
                              > > (and if you don't, you can quickly comment out regions
                              > > by putting them inside a triple-quoted string.)[/color]
                              >
                              > Although that will use up memory, as opposed to a comment.[/color]

                              Not really. Unless it is the first string in the block
                              (class, function, module), it won't be assigned to anything,
                              and will be immediately garbage-collected.

                              It may consume space in the pyc file, I'm not sure.

                              Of course, I don't think anyone would advocate leaving
                              such things in production code where the memory use
                              would be an issue anyway. The whole point of
                              block-commenting code out is to temporarily "delete" it
                              without having to use your version control system to get
                              it back. You only do that when you have strong feeling
                              you're going to need to put it back in.

                              --
                              Terry Hancock (hancock@Anansi Spaceworks.com)
                              Anansi Spaceworks http://www.AnansiSpaceworks.com

                              Comment

                              Working...