Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
Posts: 8,159
Threads: 160
Joined: Sep 2016
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 # .
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
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
Posts: 8,159
Threads: 160
Joined: Sep 2016
(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!"
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
Posts: 8,159
Threads: 160
Joined: Sep 2016
(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?
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
Posts: 8,159
Threads: 160
Joined: Sep 2016
these are exactly block comments as described in PEP8
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
|