Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comment indentations
#1
i want to write some one line comments that describe why a line or block of code is there.  the comment is too long to be appended to the right side of the code, or just looks ugly there, so it will be on a line by itself.

should the comment (the # that begins the comment) be indented to match the code or can i leave it starting in column 1 even though the code starts in column 5, 9, 13, or further to the right?

i know it works in python either way.  i am asking about style or readability.

i am not asking about appended comments or multi-line comments.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
well, PEP8 is clear:

Quote:Block Comments
Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).
Paragraphs inside a block comment are separated by a line containing a single # .
Reply
#3
but i am not asking about block comments
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I indent a comment according to what part of the code concerns. If it fits on 3-4 rows I keep it indented. Else I indent only the first line
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jan-27-2017, 07:05 AM)Skaperen Wrote: but i am not asking about block comments

Actually you are asking exactly about them.
inline comments are the one after the specific line and block comments are one or more lines of comments that are on separate line before the block of code they apply to. you are asking about single line block comments as far as I can understand:

# This is block comment
while True:
   # another block comment
   # ask user until s/he enter number between 1 and 10
   try:
       user_input = int(raw_input ("enter number between 1 and 10: "))
       if user_input in range(1,11):
           break  # exit the while loop - inline comment
   except ValueError:
       print "Incorrect input!"
Reply
#6
yes, PEP8 has its own definition based on paragraphs:
PEP8: Wrote:Block comments generally consist of one or more paragraphs built out of complete sentences, and each sentence should end in a period.
as i see it not all of my comments are like that.  less than half are.  most of mine are notations which don't fit that.  so this is why i asked.  but, ok, i can do the same indentation to the same level as the code.  that is one of the two answers i was expecting.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(Jan-27-2017, 08:27 AM)Skaperen Wrote: not all of my comments are like that. less than half are. most of mine are notations which don't fit that.
Hm, I don't understand what kind of notations/comments you have. would you share and example, like I did?
Reply
#8
some code i am rewriting today:
    close_fdi = False
    for cmd in list_of_cmds[:-1]:
        if fdi < 0:
            # this pipe is only for the first process
            ipipe = os,pipe()
            fdi, rto = ipipe
            close_fdi = True
        # this pipe is to the next process
        pipe = os.pipe()
        fdo = pipe[1]
        fds = ( fdi, fdo, fde )
        proc = multiprocessing.Process( target=child, args=( fds, cmd ) )
        proc_list.append( proc )
        proc.start()
        # for the first process, only
        if close_fdi:
            os.close(fdi)
        # for the interprocess pipes
        os.close(fdo)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
these are exactly block comments as described in PEP8
Reply
#10
I have always found PEP 8's definition of block comments odd. I have always thought of block comments as multiline comments. That is, comments long enough to be blocked off from the code.

However, in *any* language I've used that had indenting, I have always indented by comments to match the level of indentation of the code. I've been inconsistent with if/then statements, though. Sometimes the comment feels like it goes with the condition, sometimes it feels like it goes with the indented code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 139 Mar-24-2024, 04:34 PM
Last Post: deanhystad
Question remove all comment ? SpongeB0B 7 1,191 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 1,594 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Block comment in Intellij kam_uk 2 2,253 Nov-15-2020, 05:22 PM
Last Post: kam_uk
  Python 3 Elementtree and Comment First gw1500se 3 3,661 May-25-2020, 09:02 PM
Last Post: gw1500se
  Indentations macfanpl 10 4,060 Apr-27-2020, 02:31 PM
Last Post: buran
  Youtube Comment bot mateusz135 4 58,748 Mar-14-2020, 04:18 PM
Last Post: buran
  pdb says "SyntaxError: unexpected EOF" on comment pwannh 1 3,036 Nov-29-2018, 04:17 PM
Last Post: nilamo
  Help extracting comment data from multiple zip files SoulsKeeper 10 6,030 Sep-10-2018, 10:33 AM
Last Post: SoulsKeeper
  python crontab remove_all(comment="Minute*") vvarrior 1 2,738 Aug-06-2018, 12:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020