Re: commenting out blocks of code

Discussion in 'Python' started by Peter Hansen, Feb 18, 2006.

  1. Peter Hansen

    Peter Hansen Guest

    john peter wrote:
    > is there a similar mechanism in python, other than prefixing
    > the '#' character to the start of each statement i do not
    > want to execute (which gets old very quickly if one needs to
    > comment and uncomment several statements a couple of
    > times while "playing around with code" say during initial design)?


    In addition to the approach Paul has suggested, you should also consider
    using a better editor, as most decent editors should have some kind of
    "block comment/uncomment" feature that prevents that operation from
    getting old very quickly...

    Scite, for example, lets me selected a block and hit Ctrl-Q to either
    comment or uncomment the block. (It does this by prefixing each line
    with #~ instead of just #, which allows it to detect when a line is
    already so commented and reverse the operation.) Other decent editors
    have their own approach.

    -Peter
     
    Peter Hansen, Feb 18, 2006
    #1
    1. Advertising

  2. On Fri, 17 Feb 2006 19:12:01 -0500, Peter Hansen wrote:

    > john peter wrote:
    >> is there a similar mechanism in python, other than prefixing
    >> the '#' character to the start of each statement i do not
    >> want to execute (which gets old very quickly if one needs to
    >> comment and uncomment several statements a couple of
    >> times while "playing around with code" say during initial design)?

    >
    > In addition to the approach Paul has suggested, you should also consider
    > using a better editor, as most decent editors should have some kind of
    > "block comment/uncomment" feature that prevents that operation from
    > getting old very quickly...
    >
    > Scite, for example, lets me selected a block and hit Ctrl-Q to either
    > comment or uncomment the block.


    I see the developers of Scite are either newbies to GUI programming, or
    they operate in a world of their own. Ctrl-Q is reserved for Quit (or
    Exit) in every GUI API I know of.


    > (It does this by prefixing each line
    > with #~ instead of just #, which allows it to detect when a line is
    > already so commented and reverse the operation.)


    It is *easy* to detect when a line is already commented. It starts with a
    #. The ~ is superfluous.

    Commenting and uncommenting should be two different commands: the whole
    point of nested comments is that it allows you to comment a block of text
    which may already contain comments. Having one command do both commenting
    and uncommenting according to the presence or absence of semantic clues in
    the text is a recipe for failure ("No you stupid computer, I want to
    COMMENT that block, not uncomment it!!!").

    Imagine if your text editor used cntl-C for both copy and paste, somehow
    guessing whether you wanted to copy selected text or paste over it
    according to some subtle clue in the text itself. Wouldn't that be fun?



    --
    Steven.
     
    Steven D'Aprano, Feb 18, 2006
    #2
    1. Advertising

  3. Peter Hansen

    Neil Hodgson Guest

    Hi Steven,

    > I see the developers of Scite are either newbies to GUI programming, or
    > they operate in a world of their own. Ctrl-Q is reserved for Quit (or
    > Exit) in every GUI API I know of.


    I am the developer responsible for SciTE and the Ctrl+Q decision.
    Ctrl+Q is not reserved for Quit on every platform. Perhaps you have
    never used Windows where Exit does not normally have a distinct command
    key of its own but can be performed with Alt+F4 (Close) for single
    window applications. Since exiting an application is a far less common
    action than modifying the data within that application, it doesn't
    deserve one of the more easily typed key bindings. Some applications use
    Ctrl+Q but, of the applications I currently have open, Firefox, Internet
    Explorer, Explorer, iTunes, Visual Studio .NET, WordPad, QuickTime,
    PythonWin, and WinMerge don't respond to Ctrl+Q by quitting and
    Thunderbird does.
    Windows guidelines:
    http://msdn.microsoft.com/library/en-us/dnwue/html/appxB.asp

    > It is *easy* to detect when a line is already commented. It starts with a
    > #. The ~ is superfluous.


    It is not usual to change a line from being code to being a comment
    as most lines of code make no sense as English text. If you do sometimes
    want to do this, the "#" key can be pressed at the beginning of a line.
    Ctrl+Q exists to allow commenting out sections of code that you do not
    want to be active. The "~" acts to differentiate these different uses of
    one language feature and makes it easy to apply the operation over
    ranges that include comments and then invert the operation.

    > Commenting and uncommenting should be two different commands: the whole
    > point of nested comments is that it allows you to comment a block of text
    > which may already contain comments. Having one command do both commenting
    > and uncommenting according to the presence or absence of semantic clues in
    > the text is a recipe for failure ("No you stupid computer, I want to
    > COMMENT that block, not uncomment it!!!").


    Comments and commented out code are distinct. Using the same marker
    for both is ambiguous. Few languages support true nested comments.

    SciTE's implementation of Ctrl+Q is very popular although you can
    change the text inserted/removed from "#~" to "#" if you prefer and
    SciTE also includes a command to add stream style comments for languages
    like /* C++ */.

    > Imagine if your text editor used cntl-C for both copy and paste, somehow
    > guessing whether you wanted to copy selected text or paste over it
    > according to some subtle clue in the text itself. Wouldn't that be fun?


    Your imaginary command makes no sense as it is described. What are
    its full semantics? Why was it implemented this way?

    BTW, I have been implementing GUIs for over 20 years so maybe me and
    the other SciTE users are in our own world. It does feel like a
    friendly, enjoyable, comfortable world, though.

    Neil
     
    Neil Hodgson, Feb 18, 2006
    #3
  4. Peter Hansen

    Atanas Banov Guest

    Neil Hodgson wrote:
    > Hi Steven,


    > > It is *easy* to detect when a line is already commented. It starts with a
    > > #. The ~ is superfluous.

    >
    > It is not usual to change a line from being code to being a comment
    > as most lines of code make no sense as English text. If you do sometimes
    > want to do this, the "#" key can be pressed at the beginning of a line.
    > Ctrl+Q exists to allow commenting out sections of code that you do not
    > want to be active. The "~" acts to differentiate these different uses of
    > one language feature and makes it easy to apply the operation over
    > ranges that include comments and then invert the operation.


    hi.
    here is something you both seems to have not considered: imagine you
    make decision if ^Q has to comment or uncomment based on the 1st line
    and not on each line individually in the block. so let's say i mark and
    ^Q this:
    ------------------
    code1
    #comment1
    #comment2
    code2
    ----------------

    1st line is not commented, so i want to comment-out the whole blcok,
    the result is
    ------------------
    #code1
    ##comment1
    ##comment2
    #code2
    ----------------

    note how ## maintains where comments were. now, for the same selection,
    ^Q again? 1st character is #, so the editor is asked to uncomment,
    dwim:
    ------------------
    code1
    #comment1
    #comment2
    code2
    ----------------

    so there is a way to do it without #~ noise... now if we can only think
    of how to do DWIM copy&paste :)!

    - nas
     
    Atanas Banov, Feb 18, 2006
    #4
  5. Peter Hansen

    Duncan Booth Guest

    Steven D'Aprano wrote:

    >> Scite, for example, lets me selected a block and hit Ctrl-Q to either
    >> comment or uncomment the block.

    >
    > I see the developers of Scite are either newbies to GUI programming, or
    > they operate in a world of their own. Ctrl-Q is reserved for Quit (or
    > Exit) in every GUI API I know of.


    Your experience obviously differs from mine. I just tried Ctrl-Q on both
    windows and ubuntu and in neither case did it exit applications
    consistently: For example Firefox uses Alt-F4 in both cases.

    My own favourite editor (Epsilon) uses Ctrl-Q for the 'quoted-insert'
    command (hardly suprising given its similarity to emacs).

    >> (It does this by prefixing each line
    >> with #~ instead of just #, which allows it to detect when a line is
    >> already so commented and reverse the operation.)

    >
    > It is *easy* to detect when a line is already commented. It starts with a
    > #. The ~ is superfluous.
    >

    Not so easy if the lines to be commented already contain some lines
    starting with comments. Messing around with the comments themselves sounds
    highly unsatisfactory though.

    I agree with you that the best solution is to use different commands (or as
    Epsilon does a single command which can be modified with a prefix Ctrl-U).
    That way I can select a large region and comment it all out, or uncomment
    disjoint parts of it as I choose.
     
    Duncan Booth, Feb 18, 2006
    #5
  6. On Sat, 18 Feb 2006 03:14:49 +0000, Neil Hodgson wrote:

    > I am the developer responsible for SciTE and the Ctrl+Q decision.
    > Ctrl+Q is not reserved for Quit on every platform.


    [snip]


    Thank you for your thoughtful response. Although I'm not convinced by your
    reasoning, I will concede that perhaps my post was a little hasty.


    --
    Steven.
     
    Steven D'Aprano, Feb 18, 2006
    #6
  7. Peter Hansen

    Steve Holden Guest

    Steven D'Aprano wrote:
    > On Fri, 17 Feb 2006 19:12:01 -0500, Peter Hansen wrote:
    >
    >
    >>john peter wrote:
    >>
    >>>is there a similar mechanism in python, other than prefixing
    >>>the '#' character to the start of each statement i do not
    >>>want to execute (which gets old very quickly if one needs to
    >>>comment and uncomment several statements a couple of
    >>>times while "playing around with code" say during initial design)?

    >>
    >>In addition to the approach Paul has suggested, you should also consider
    >>using a better editor, as most decent editors should have some kind of
    >>"block comment/uncomment" feature that prevents that operation from
    >>getting old very quickly...
    >>
    >>Scite, for example, lets me selected a block and hit Ctrl-Q to either
    >>comment or uncomment the block.

    >
    >
    > I see the developers of Scite are either newbies to GUI programming, or
    > they operate in a world of their own. Ctrl-Q is reserved for Quit (or
    > Exit) in every GUI API I know of.
    >

    I don't think this is common enough to regard as any kind of de facto
    standard.

    Just to give two further counterexamples, neither TextPad nor Notepad
    action a Ctrl/Q in that way. Perhaps I'm just using the wrong operating
    system?
    >
    >
    >>(It does this by prefixing each line
    >>with #~ instead of just #, which allows it to detect when a line is
    >>already so commented and reverse the operation.)

    >
    >
    > It is *easy* to detect when a line is already commented. It starts with a
    > #. The ~ is superfluous.
    >
    > Commenting and uncommenting should be two different commands: the whole
    > point of nested comments is that it allows you to comment a block of text
    > which may already contain comments. Having one command do both commenting
    > and uncommenting according to the presence or absence of semantic clues in
    > the text is a recipe for failure ("No you stupid computer, I want to
    > COMMENT that block, not uncomment it!!!").
    >
    > Imagine if your text editor used cntl-C for both copy and paste, somehow
    > guessing whether you wanted to copy selected text or paste over it
    > according to some subtle clue in the text itself. Wouldn't that be fun?
    >

    I agree that's a less-than-sensible feature implementation.

    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/
     
    Steve Holden, Feb 18, 2006
    #7
  8. Peter Hansen

    Peter Hansen Guest

    Steve Holden wrote:
    > Steven D'Aprano wrote:
    >>On Fri, 17 Feb 2006 19:12:01 -0500, Peter Hansen wrote:
    >>>Scite, for example, lets me selected a block and hit Ctrl-Q to either
    >>>comment or uncomment the block.
    >>>(It does this by prefixing each line
    >>>with #~ instead of just #, which allows it to detect when a line is
    >>>already so commented and reverse the operation.)

    >>
    >>It is *easy* to detect when a line is already commented. It starts with a
    >>#. The ~ is superfluous.
    >>
    >>Commenting and uncommenting should be two different commands: the whole
    >>point of nested comments is that it allows you to comment a block of text
    >>which may already contain comments. Having one command do both commenting
    >>and uncommenting according to the presence or absence of semantic clues in
    >>the text is a recipe for failure ("No you stupid computer, I want to
    >>COMMENT that block, not uncomment it!!!").
    >>
    >>Imagine if your text editor used cntl-C for both copy and paste, somehow
    >>guessing whether you wanted to copy selected text or paste over it
    >>according to some subtle clue in the text itself. Wouldn't that be fun?

    >
    > I agree that's a less-than-sensible feature implementation.


    I submit that Steve and Steven (neither of whom has apparently even
    *used* this feature of Scite before criticizing it so strongly), should
    consider whether in actual practice something like this might not be
    much more effective than they can apparently imagine.

    I'm strongly critical of many features of most editors, and thus use
    very few of them, and I also happen to have a solid background in GUI
    design and ergonomics. I'm also a heavy user of Scite, and have found
    it readily accepted amongst the groups of programmers with whom I've worked.

    I and my team have found the Ctrl+Q feature of Scite to be very
    sensible, highly effective and useful, and in fact *better* than
    alternatives that we've used before in other editors.

    Kudos to Neil for having the (apparently) unusual design sense to think
    of it and implement it!

    So there. Harumph. :)

    -Peter
     
    Peter Hansen, Feb 19, 2006
    #8
  9. Peter Hansen

    Neil Hodgson Guest

    Atanas Banov:

    > here is something you both seems to have not considered: imagine you
    > make decision if ^Q has to comment or uncomment based on the 1st line
    > and not on each line individually in the block.


    When first implementing Ctrl+Q, this looked to have similar
    advantages and disadvantages to the chosen implementation. However, it
    wouldn't allow a technique I've come to use which is to have two
    versions of some code or section of a configuration file and to toggle
    between the two by selecting the whole range and pressing Ctrl+Q.

    > #code1
    > ##comment1
    > ##comment2
    > #code2
    > ----------------
    >
    > note how ## maintains where comments were. now, for the same selection,
    > ^Q again? 1st character is #, so the editor is asked to uncomment,
    > dwim:


    The problem with "##" as opposed to "#~" is that "##" is common in
    much source code particularly in barrier comments and comment sections:

    ############ Persistence section ############
    ## These functions are responsible for load
    ## and save for the core media database.

    Neil
     
    Neil Hodgson, Feb 20, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Commenting out VB code

    =?Utf-8?B?U2FuZHk=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    52,004
    =?Utf-8?B?Sm9uWg==?=
    Feb 3, 2005
  2. patrick

    java commenting out of code

    patrick, Jan 11, 2006, in forum: Java
    Replies:
    4
    Views:
    5,282
    Malte Christensen
    Jan 11, 2006
  3. Paul McNett

    Re: commenting out blocks of code

    Paul McNett, Feb 18, 2006, in forum: Python
    Replies:
    1
    Views:
    642
    Cameron Laird
    Feb 18, 2006
  4. Jean-Paul Calderone

    Re: commenting out blocks of code

    Jean-Paul Calderone, Feb 18, 2006, in forum: Python
    Replies:
    0
    Views:
    502
    Jean-Paul Calderone
    Feb 18, 2006
  5. matt
    Replies:
    1
    Views:
    296
    George Ogata
    Aug 6, 2004
Loading...

Share This Page