469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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?
Mar 8 '06 #1
18 1938
John Salerno wrote:
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?


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>

Mar 8 '06 #2
Fredrik Lundh <fr*****@pythonware.com> wrote:
you can quickly comment out regions by putting them
inside a triple-quoted string.)


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.
Mar 8 '06 #3
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. :)

Mar 8 '06 #4
....and I forgot to mention that the output of grep and diff is far more
understandable in the absence of block comments!

Mar 8 '06 #5
Warby wrote:
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.


That makes sense. If you have a modern editor, you don't need blocks. If
you don't have one, blocks don't help. :)
Mar 8 '06 #6
Roy Smith wrote:
you can quickly comment out regions by putting them
inside a triple-quoted string.)


Except that triple-quotes don't nest.


''' """...except when they do""" '''

</F>

Mar 8 '06 #7
Warby <Ma************@gmail.com> wrote:
Eliminating block comments eliminates uncertainty. :)


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.

Mar 8 '06 #8
On Wednesday 08 March 2006 12:42 pm, Warby wrote:
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).


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 anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
Mar 9 '06 #9
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.

Mar 9 '06 #10
Terry Hancock wrote:
I'm thinking this might be a use-case for the new support for
doctests in a separate file.


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
Mar 9 '06 #11
> (and if you don't, you can quickly comment out regions by putting them
inside a triple-quoted string.)


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

Mar 9 '06 #12
msoulier <ms******@gmail.com> wrote:
(and if you don't, you can quickly comment out regions by putting them
inside a triple-quoted string.)


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


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.
Mar 9 '06 #13
msoulier wrote:
(and if you don't, you can quickly comment out regions by putting them
inside a triple-quoted string.)


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


Doesn't seem so:
def f(): .... "docstring"
.... "another string"
.... a = 42
.... "yet another string"
.... f.func_code.co_consts ('docstring', 42, None)


Peter
Mar 9 '06 #14
On 9 Mar 2006 07:21:00 -0800
"msoulier" <ms******@gmail.com> wrote:
(and if you don't, you can quickly comment out regions
by putting them inside a triple-quoted string.)


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


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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 10 '06 #15
On Thu, 09 Mar 2006 18:02:27 -0600, Terry Hancock wrote:
On 9 Mar 2006 07:21:00 -0800
"msoulier" <ms******@gmail.com> wrote:
> (and if you don't, you can quickly comment out regions
> by putting them inside a triple-quoted string.)


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


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.


I don't believe this is true. Unassigned strings other than the doc string
are not compiled into the code:
def f(x): .... "this is a doc string"
.... "but this isn't"
.... return "hello world"
.... dis.dis(f) 4 0 LOAD_CONST 1 ('hello world')
3 RETURN_VALUE
4 LOAD_CONST 2 (None)
7 RETURN_VALUE
Strangely enough, this is a local optimization that appears to have been
done only for strings:
def g(): .... 45
.... return 55
.... dis.dis(g)

2 0 LOAD_CONST 1 (45)
3 POP_TOP

3 4 LOAD_CONST 2 (55)
7 RETURN_VALUE
8 LOAD_CONST 0 (None)
11 RETURN_VALUE

So you should feel free to use strings (triple-quoted or otherwise) as
documentation in your functions.

--
Steven.

Mar 10 '06 #16
On Sat, 11 Mar 2006 10:23:56 +1100
"Steven D'Aprano" <st***@REMOVETHIScyber.com.au> wrote:
On Thu, 09 Mar 2006 18:02:27 -0600, Terry Hancock wrote:
On 9 Mar 2006 07:21:00 -0800
"msoulier" <ms******@gmail.com> wrote:
> (and if you don't, you can quickly comment out

regions > > by putting them inside a triple-quoted
string.) >
Although that will use up memory, as opposed to a

comment.

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.


I don't believe this is true. Unassigned strings other
than the doc string are not compiled into the code:


[bytecode analysis snipped]

Cool. I thought that was probably true, but didn't want
to guess.

Cheers,
Terry
--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 11 '06 #17
Warby wrote:
...and I forgot to mention that the output of grep and diff is far more
understandable in the absence of block comments!


Which is why people do this /anyway/. (Kind of makes block comments
pointless, doesn't it?

/* This is a
* really
* really
* long
* block comment */

Mar 11 '06 #18
In article <11*********************@v46g2000cwv.googlegroups. com>,
"Jonathan Gardner" <jg******@jonathangardner.net> wrote:
Warby wrote:
...and I forgot to mention that the output of grep and diff is far more
understandable in the absence of block comments!


Which is why people do this /anyway/. (Kind of makes block comments
pointless, doesn't it?

/* This is a
* really
* really
* long
* block comment */


Habit left over from the C days. It was the only way of making a block
comment stand out visually. C++ has // comments, just like Python has #,
but old habits die hard.
Mar 11 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Tim Parkin | last post: by
699 posts views Thread by mike420 | last post: by
9 posts views Thread by Adam Barr | last post: by
3 posts views Thread by MartinRinehart | last post: by
3 posts views Thread by Richard | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.