how to comment lot of lines in python

Discussion in 'Python' started by diffuser78, Mar 31, 2006.

  1. diffuser78

    diffuser78 Guest

    Like in C we comment like
    Bunch of lines of code

    Should we use docstring """ """

    Or there is something else too ??

    Every help is appreciated.

    diffuser78, Mar 31, 2006
    1. Advertisements

  2. Em Qui, 2006-03-30 às 15:21 -0800, escreveu:
    You should use a decent editor that could automatically
    comment/uncomment code upon your request.

    Felipe Almeida Lessa, Mar 31, 2006
    1. Advertisements

  3. diffuser78

    Tim Chase Guest

    Or there is something else too ??
    In Vim, you can map a key to do the following:


    to comment the highlighted lines, and


    to uncomment them. To map them, you can use

    :vnoremap <f4> :s/^/#<cr>
    :vnoremap <f5> :s/#<cr>

    (those are literal "less-than, ["eff, [4 | 5]" | "see,
    are"], greater-than" characters)

    Then, pressing <f4> in visual mode will comment the selected
    lines, and pressing <f5> will uncomment any selected lines
    that are commented.

    The nice thing about vim's method of doing this, is that you
    can combine it with grepping. If you want to comment out
    every line containing a regexp, you can do


    Or, if you want to comment out from "regexp_1" to the
    following line containing "regexp_2", you can use


    All sorts of handy tricks.

    There are python plugins that I'm sure offer such abilities
    built-in. There are likely ways to do this in other editors
    too. I just happen to be a vim sorta guy.

    Tim Chase, Mar 31, 2006
  4. Hi,

    Maybe this sounds simplier than regexp and so, just use the """ marker like
    this :

    would be
    Laurent Rahuel, Mar 31, 2006
  5. diffuser78

    Eric Deveaud Guest

    I would say NO.
    docstring are displayed by pydoc, thus a pydoc on your code will display some
    inconsistent information ;-)
    some moderns editors allow you to comment/uncomment a selected Bunch
    of lines of code

    JPH in Guide du linuxien pervers : "Connaître le système"
    Eric Deveaud, Mar 31, 2006
  6. diffuser78

    olsongt Guest

    docstrings are a bit of a magical construct. Not all strings in a
    function are docstrings.
    .... "real docstring"
    .... """
    .... x=1
    .... """
    .... print x
    ....Help on function foo in module __main__:

    real docstring
    olsongt, Mar 31, 2006
  7. diffuser78

    Eric Deveaud Guest

    yep fogotten that triple quotted strings are considered as docstring
    only if they are the first lines of the module/fonction/class/method
    excluding the comments lines.

    my bad

    Eric Deveaud, Mar 31, 2006
  8. The official rule is that if *any* string is the first line of a
    function/etc, it is considered a docstring. It's just standard
    convention to use the triple quotes for docstrings. As you mentioned,
    you can use triple quotes for any string; likewise, you can use standard
    quotes ( ' or " ) for docstrings as well.

    - Mike
    Michael Hobbs, Mar 31, 2006
  9. I often use

    if 0:
    bunch of lines of code

    That way, it's very easy to reenable the code, or to add an else, etc.
    I can even put things like 'if 0 and USE_FOO_FEATURE' to document what
    is being commented out. It's much more flexible than commenting out.
    Dave Mandelin, Mar 31, 2006
  10. diffuser78

    gene tani Guest

    gene tani, Mar 31, 2006
  11. Out of curiousity, is there a modern editor which *doesn't* allow you
    to comment/uncomment a selected bunch of lines of code?
    Sion Arrowsmith, Apr 3, 2006
  12. diffuser78

    Kent Johnson Guest

    TextPad is my editor of choice but it doesn't have this feature.

    Kent Johnson, Apr 3, 2006
  13. diffuser78

    Rick Zantow Guest

    I use TextPad all the time, and while it is true that the feature is not
    built in, it's misleading to say it doesn't have it. It is implemented by
    means of a macro definition. I assign a key to a 'comment' macro (basically
    replacing regex ^ with '# ' for the selected text) to do this. And of
    course I have a reciprocal 'uncomment' macro as well.
    Rick Zantow, Apr 3, 2006
  14. I also use TextPad - 4.7.3. I have never thought to comment a block of
    code, but this thread prompted me to see if it is possible. As far as I
    can see, you can do it like this -

    Menu > Configure > Block Select Mode - Ctrl+Q,B

    Select a block, one column wide, using mouse or keyboard.

    Menu > Edit > Fill Block (brings up dialog box)
    Fill character: #
    Format: Left align
    Fill mode: Replace


    To uncomment, as above but set Fill character to a space.

    It feels a bit fiddly, but I reckon if you use it a few times it will
    become quite smooth.

    Frank Millman
    Frank Millman, Apr 3, 2006
  15. diffuser78

    Kent Johnson Guest

    Thank you! I don't suppose you have any tricks to make it work with
    UTF-8 data outside the cp1252 character set, do you?

    Kent Johnson, Apr 3, 2006
  16. diffuser78

    Rick Zantow Guest

    Sadly, I don't. TP claims Unicode support, but I'm not sure what they
    mean; it does seem to be restricted to cp1252.
    Rick Zantow, Apr 4, 2006
  17. diffuser78

    Kent Johnson Guest

    TP will correctly read and write UTF-8 files if they use only the
    characters available in cp1252. So there is a little Unicode support but
    it breaks down pretty quickly with, e.g. Chinese or even Polish.

    Kent Johnson, Apr 4, 2006
  18. diffuser78

    jussij Guest

    You should use a decent editor that could automatically
    The Zeus for Windows IDE has just such a feature:

    To do this in Zeus you basically mark the lines of text
    that need commenting then use the Macros, Add Comment
    Block menu to comment the lines selected.

    Then to remove the comments you use the Macros, Remove
    Comment Block menu.

    Jussi Jumppanen
    Author: Zeus for Windows
    jussij, Apr 5, 2006
  19. What's wrong with using three sets of double-quotes? I do it with kwrite
    and it works like a charm.
    Michael Sperlle, Apr 5, 2006
  20. One can't comment out code that contains multiline strings. Especially
    nested comment out does not work.
    Why not Ctrl+D (comment) and Ctrl+Shift+D (uncomment)?

    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Apr 5, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.