Single Pound # comments versus Double Pound ## comments

Terry Hancock hancock at anansispaceworks.com
Fri Oct 4 19:45:10 EDT 2002


Jeff Epler wrote:
> On Fri, Oct 04, 2002 at 04:04:00PM -0700, Terry Hancock wrote:
> > this_is_a_dict = {
> >                    'spam':1,
> > ###
>   # Didn't really want this part, but might later:
> >
> >                    'ham':3,
> >                    'eggs':4,
> > ###
> >                    'morespam':2
> >                  }
> 
> How is this "more unambiguous", given that the code fragment above has a
> meaning today?

Okay, I'll bite -- what does it mean?  I've never seen
it in use (which doesn't mean it doesn't happen of course),
and searching python.org for "###" is unhelpful.  Maybe
this means I'm being really dumb. If so, "touche, your
point", but could you be a bit more explicit please?

Seems to me the "###" would currently just get thrown out,
leaving the dictionary unedited.  Clearly code that relied
on this would be incompatible with earlier versions of the
interpreter, but that's a different issue.

The problem with using """ here, of course, is that it
means something completely different from what you would've
meant:

Everything between """ and """ will get marked as string,
concatenated with the string 'morespam' following it, and
mapped to that value (i.e. 2).  A comment shouldn't do
that!  Thus, recommending that blind strings be used to
document code has potentially serious pitfalls.

As it stands, if I want this behavior, I can only acheive
it by editing every line:

this_is_a_dict = {
                  'spam':1,
# Didn't really want this part, but might later:
#                    'ham':3,
#                    'eggs':4,
                  'morespam':2
                 }

Which is not too bad for a two-line example, but can
be quite obnoxious for testing larger pieces of code.
True, I could rely on a smarter editor, but that's not
always convenient. The thing that particularly bothers
me about this edit-every-line to comment out the code
and edit-every-line to reactivate the code, is that it
introduces a chance of making an error on every effected
line at each test cycle.  Since Python relies on indentation,
it is very easy to accidently screw up the indentation
this way and introduce a bug during testing.

That's particularly annoying since the main reason
I probably commented the code out was to test with and
without the code.  Having to edit each line makes it
less certain that I haven't altered the code between
tests. Of course, I could probably use """ for those
cases, and # for comments within expressions (as above).
But that's kind of inconsistent (two completely different
techniques for almost the same situation).

As it is, if I have more than say 10 lines of code to
do this with, I actually cut and paste the code to and
from another editor window, which is a nasty workaround.

Cheers,
Terry

-- 
------------------------------------------------------
Terry Hancock
hancock at anansispaceworks.com       
Anansi Spaceworks                 
http://www.anansispaceworks.com 
P.O. Box 60583                     
Pasadena, CA 91116-6583
------------------------------------------------------




More information about the Python-list mailing list