triple quoted strings as comments

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

    #1

    triple quoted strings as comments

    I recently complained elsewhere that Python doesn't have multiline
    comments. i was told to use triple quoted strings to make multiline
    comments. My question is that since a triple quoted string is actually
    a language construct, does it use cause a runtime construction of a
    string which is then discarded, or is the runtime smart enough to see
    that it isn't used and so it doesn't construct it?

    example

    def fun(self):
    """doc comment
    comment line 2
    """

    x = 1
    y = 2

    """does this triple quoted string used as a comment
    cause something to happen at runtime beyond
    just skipping over it? Such as allocation of memory for a string
    or worse yet garbage collection? or not?
    """
    z = x + y

    ....

    dave howard

  • mithrond@gmail.com

    #2
    Re: triple quoted strings as comments

    as i know, the triple quoted string does cause a runtime construction,
    and will not be discarded, and it's a benefit of python language.
    here is sth. useful.
    _http://diveintopython. org/power_of_intros pection/index.html

    best regard

    Comment

    • Peter Hansen

      #3
      Re: triple quoted strings as comments

      dmh2000 wrote:[color=blue]
      > I recently complained elsewhere that Python doesn't have multiline
      > comments. i was told to use triple quoted strings to make multiline
      > comments. My question is that since a triple quoted string is actually
      > a language construct, does it use cause a runtime construction of a
      > string which is then discarded, or is the runtime smart enough to see
      > that it isn't used and so it doesn't construct it?[/color]

      Easy enough to find out. Put this in test.py:

      'first module comment'
      'another'

      def func():
      'first func comment'
      'another'
      print 'hi'
      'last one'

      'and last module comment'


      Now do "import test" from the Python prompt, then exit the interpreter
      and do "strings test.pyc" on the compiled bytecode.

      (The short answer is only the first such comments are kept.)

      -Peter

      Comment

      • André

        #4
        Re: triple quoted strings as comments

        dmh2000 wrote:[color=blue]
        > I recently complained elsewhere that Python doesn't have multiline
        > comments. i was told to use triple quoted strings to make multiline
        > comments. My question is that since a triple quoted string is actually
        > a language construct, does it use cause a runtime construction of a
        > string which is then discarded, or is the runtime smart enough to see
        > that it isn't used and so it doesn't construct it?
        >
        > example
        >
        > def fun(self):
        > """doc comment
        > comment line 2
        > """
        >
        > x = 1
        > y = 2
        >
        > """does this triple quoted string used as a comment
        > cause something to happen at runtime beyond
        > just skipping over it? Such as allocation of memory for a string
        > or worse yet garbage collection? or not?
        > """
        > z = x + y[/color]
        It seems to discard the second triple quoted comment (the first one is
        kept around as a doc string).
        I created two scripts, one with the second triple quoted string, the
        other without. The compiled version is *almost* the same (one byte
        difference which, if I am not mistaken, comes from the different
        filename embedded in the .pyc file).

        30/01/2006 09:34 PM 327 triple.py
        30/01/2006 09:35 PM 359 triple.pyc
        30/01/2006 09:34 PM 96 triple2.py
        30/01/2006 09:35 PM 358 triple2.pyc
        André

        Comment

        • Terry Hancock

          #5
          Re: triple quoted strings as comments

          On 30 Jan 2006 16:29:15 -0800
          "dmh2000" <dmh2000@gmail. com> wrote:[color=blue]
          > I recently complained elsewhere that Python doesn't have
          > multiline comments. i was told to use triple quoted
          > strings to make multiline comments. My question is that
          > since a triple quoted string is actually a language
          > construct, does it use cause a runtime construction of a
          > string which is then discarded, or is the runtime smart
          > enough to see that it isn't used and so it doesn't
          > construct it?
          >
          > example
          >
          > def fun(self):
          > """doc comment
          > comment line 2
          > """[/color]

          This is specifically a "docstring" so it remains attached as
          an attribute of fun: fun.__doc__
          [color=blue]
          > x = 1
          > y = 2
          >
          > """does this triple quoted string used as a comment
          > cause something to happen at runtime beyond
          > just skipping over it? Such as allocation of memory
          > for a string or worse yet garbage collection? or
          > not?
          > """[/color]

          This string is really unused. It will produce a value when
          processed the first time, but it's not bound so it gets
          immediately garbage-collected. And it won't be there after
          the module is byte-compiled. So, you lose a little time the
          very first time the file is used (but that's technically
          true for a regular comment too -- I think this loses you a
          little more time). But it's pretty trivial in practice,
          because every subsequent time, it's gone.
          [color=blue]
          > z = x + y[/color]


          At least, this is how I understand it.

          Cheers,
          Terry


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

          Comment

          • Roy Smith

            #6
            Re: triple quoted strings as comments

            "dmh2000" <dmh2000@gmail. com> wrote:[color=blue]
            > I recently complained elsewhere that Python doesn't have multiline
            > comments.[/color]

            Of course it has multi-line comments. They look like this:

            # This is the first line
            # and this is the second.

            Why is that a problem?

            Comment

            • Steve Holden

              #7
              Re: triple quoted strings as comments

              dmh2000 wrote:[color=blue]
              > I recently complained elsewhere that Python doesn't have multiline
              > comments.[/color]

              Personally I think it's a win that you couldn't find anything more
              serious to complain about :-)

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC www.holdenweb.com
              PyCon TX 2006 www.python.org/pycon/

              Comment

              • Duncan Booth

                #8
                Re: triple quoted strings as comments

                dmh2000 wrote:
                [color=blue]
                > example
                >
                > def fun(self):
                > """doc comment
                > comment line 2
                > """
                >
                > x = 1
                > y = 2
                >
                > """does this triple quoted string used as a comment
                > cause something to happen at runtime beyond
                > just skipping over it? Such as allocation of memory for a string
                > or worse yet garbage collection? or not?
                > """
                > z = x + y
                >[/color]

                How to find out for yourself:
                [color=blue][color=green][color=darkred]
                >>> def fun(self):[/color][/color][/color]
                """doc comment
                comment line 2
                """

                x = 1
                y = 2

                """does this triple quoted string used as a comment
                cause something to happen at runtime beyond
                just skipping over it? Such as allocation of memory for a string
                or worse yet garbage collection? or not?
                """
                z = x + y

                [color=blue][color=green][color=darkred]
                >>> import dis
                >>> dis.dis(fun)[/color][/color][/color]
                6 0 LOAD_CONST 1 (1)
                3 STORE_FAST 2 (x)

                7 6 LOAD_CONST 2 (2)
                9 STORE_FAST 1 (y)

                14 12 LOAD_FAST 2 (x)
                15 LOAD_FAST 1 (y)
                18 BINARY_ADD
                19 STORE_FAST 3 (z)
                22 LOAD_CONST 3 (None)
                25 RETURN_VALUE[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                Further inspection shows that it hasn't even saved that second string as a
                constant:
                [color=blue][color=green][color=darkred]
                >>> print fun.func_code.c o_consts[/color][/color][/color]
                ('doc comment\n comment line 2\n ', 1, 2, None)

                Comment

                • Jeffrey Schwab

                  #9
                  Re: triple quoted strings as comments

                  Steve Holden wrote:[color=blue]
                  > dmh2000 wrote:
                  >[color=green]
                  >> I recently complained elsewhere that Python doesn't have multiline
                  >> comments.[/color]
                  >
                  >
                  > Personally I think it's a win that you couldn't find anything more
                  > serious to complain about :-)[/color]

                  +1 QOTW

                  Comment

                  • Magnus Lycka

                    #10
                    Re: triple quoted strings as comments

                    dmh2000 wrote:[color=blue]
                    > I recently complained elsewhere that Python doesn't have multiline
                    > comments.[/color]
                    It seems you have a bad editor if it can't conveniently
                    add and remove comment markers for arbitrary blocks in
                    your source. (Maybe you just didn't find this feature.)

                    That every comment line begins with a special symbol just
                    adds clarity. It's a bonus. C++ introduced // as comment
                    symbol for this purpose (but was stuck with /* and */ due
                    to backward compatibility reasons). Ada, while otherwise
                    similar to Pascal, adopted -- to end of row instead of (*
                    and *) etc.

                    An editor that adds/removes '# ' in the beginning of each
                    marked line is fairly bullet proof. Adding e.g. /* to the
                    beginning of a block you want to comment out, & */ to the
                    end, breaks if you have /* */ style comments in the block!

                    Comment

                    • Roy Smith

                      #11
                      Re: triple quoted strings as comments

                      Magnus Lycka <lycka@carmen.s e> wrote:[color=blue]
                      > An editor that adds/removes '# ' in the beginning of each
                      > marked line is fairly bullet proof. Adding e.g. /* to the
                      > beginning of a block you want to comment out, & */ to the
                      > end, breaks if you have /* */ style comments in the block![/color]

                      /* */ also allows for some truly spectacularly bad coding practices. Not
                      long ago, I ran into some real-life code where a previous developer had
                      commented out about 50 lines of C++ code by just putting a /* at the top
                      and a */ at the bottom. I was tearing my hair out trying to figure out how
                      the code worked until I noticed what he had done.

                      Comment

                      • Roel Schroeven

                        #12
                        Re: triple quoted strings as comments

                        Roy Smith schreef:[color=blue]
                        > Magnus Lycka <lycka@carmen.s e> wrote:[color=green]
                        >> An editor that adds/removes '# ' in the beginning of each
                        >> marked line is fairly bullet proof. Adding e.g. /* to the
                        >> beginning of a block you want to comment out, & */ to the
                        >> end, breaks if you have /* */ style comments in the block![/color]
                        >
                        > /* */ also allows for some truly spectacularly bad coding practices. Not
                        > long ago, I ran into some real-life code where a previous developer had
                        > commented out about 50 lines of C++ code by just putting a /* at the top
                        > and a */ at the bottom. I was tearing my hair out trying to figure out how
                        > the code worked until I noticed what he had done.[/color]

                        That happened to me a few times. These days I use an editor with syntax
                        highlighting that shows comments in another color than code. That helps
                        tremendously.

                        --
                        If I have been able to see further, it was only because I stood
                        on the shoulders of giants. -- Isaac Newton

                        Roel Schroeven

                        Comment

                        • Jorgen Grahn

                          #13
                          Re: triple quoted strings as comments

                          On Wed, 01 Feb 2006 13:41:33 GMT, Roel Schroeven <rschroev_nospa m_ml@fastmail.f m> wrote:[color=blue]
                          > Roy Smith schreef:[/color]
                          ....[color=blue][color=green]
                          >> /* */ also allows for some truly spectacularly bad coding practices. Not
                          >> long ago, I ran into some real-life code where a previous developer had
                          >> commented out about 50 lines of C++ code by just putting a /* at the top
                          >> and a */ at the bottom. I was tearing my hair out trying to figure out how
                          >> the code worked until I noticed what he had done.[/color]
                          >
                          > That happened to me a few times. These days I use an editor with syntax
                          > highlighting that shows comments in another color than code. That helps
                          > tremendously.[/color]

                          Syntax highlighting, same here. Plus version control so you can see who did
                          it, and make a mental note not to trust that person in the future ;-)

                          ("#if 0" in C and C++ are better choices, but only marginally. Best is to
                          remove the code unless you are sure it's needed again soon. Works in all
                          languages.)

                          /Jorgen

                          --
                          // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
                          \X/ snipabacken.dyn dns.org> R'lyeh wgah'nagl fhtagn!

                          Comment

                          • Volker Grabsch

                            #14
                            Re: triple quoted strings as comments

                            Jorgen Grahn wrote:[color=blue][color=green][color=darkred]
                            >>> [...] developer had
                            >>> commented out about 50 lines of C++ code by just putting a /* at the top
                            >>> and a */ at the bottom.[/color][/color][/color]
                            [...][color=blue]
                            > ("#if 0" in C and C++ are better choices, but only marginally. Best is to
                            > remove the code unless you are sure it's needed again soon. Works in all
                            > languages.)[/color]

                            However, I'd only advise to do this if you are using a revision control.
                            Otherwise, you'll end up having a lot of "backup" files hanging around
                            which are even worse than multi-row comments. Or, even worse: If you
                            don't create backup files before removing code ...


                            Greets,

                            Volker

                            --
                            Volker Grabsch
                            ---<<(())>>---
                            \frac{\left|\va rtheta_0\times\ {\ell,\kappa\in \Re\}\right|}{\ sqrt
                            [G]{-\Gamma(\alpha)\ cdot\mathcal{B} ^{\left[\oint\!c_\hbar\ right]}}}

                            Comment

                            • Jorgen Grahn

                              #15
                              Re: triple quoted strings as comments

                              On 6 Feb 2006 12:53:53 GMT, Volker Grabsch <volker_grabsch @v.notjusthosti ng.com> wrote:[color=blue]
                              > Jorgen Grahn wrote:[color=green][color=darkred]
                              >>>> [...] developer had
                              >>>> commented out about 50 lines of C++ code by just putting a /* at the top
                              >>>> and a */ at the bottom.[/color][/color]
                              > [...][color=green]
                              >> ("#if 0" in C and C++ are better choices, but only marginally. Best is to
                              >> remove the code unless you are sure it's needed again soon. Works in all
                              >> languages.)[/color]
                              >
                              > However, I'd only advise to do this if you are using a revision control.[/color]

                              But surely, everybody's using revision control these days?
                              (Feel free to smile at my naivety now ;-)

                              /Jorgen

                              --
                              // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
                              \X/ snipabacken.dyn dns.org> R'lyeh wgah'nagl fhtagn!

                              Comment

                              Working...