why no block comments in Python?

J

John Salerno

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?
 
F

Fredrik Lundh

John said:
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>
 
R

Roy Smith

Fredrik Lundh said:
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.
 
W

Warby

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. :)
 
W

Warby

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

John Salerno

Warby said:
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. :)
 
R

Roy Smith

Warby said:
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.
 
T

Terry Hancock

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.
 
P

Paddy

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.
 
B

Benji York

Terry said:
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.
 
M

msoulier

(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
 
P

Peter Otten

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

Doesn't seem so:
.... "docstring"
.... "another string"
.... a = 42
.... "yet another string"
....
Peter
 
R

Roy Smith

msoulier said:
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.
 
T

Terry Hancock

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.
 
S

Steven D'Aprano

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:
.... "this is a doc string"
.... "but this isn't"
.... return "hello world"
.... 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:
.... 45
.... return 55
.... 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.
 
T

Terry Hancock

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
 
J

Jonathan Gardner

Warby said:
...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 */
 
R

Roy Smith

"Jonathan Gardner said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top