Search code examples
pythoncommentspep8

Commenting python lines that are code, which style to use?


Let's say I have the following python snippet:

if True:
    print("a")
    print("b")
    print("c")
else:
    print("d")

I am making an editor which has a feature to toggle comment on selected lines, but I cannot decide whether to use

# if True:
    # print("a")
    # print("b")
    # print("c")
# else:
    # print("d")

or

# if True:
#     print("a")
#     print("b")
#     print("c")
# else:
#     print("d")

I am more inclined to use the second one, as it will also make is easier for opening the file in another editor that has column-wise editing capabilities.

Whereas the first one makes it more readable when commenting parts in the middle:

if True:
    print("a")
    # print("b")
    print("c")
else:
    print("d")

I was wondering if there is anything like pep8 for this case as well. I couldn't find anything there or on google.

Thanks!


Solution

  • As per pep8 there is guide for multi-line comments but only for doc string and other useful information for code following the comments.

    However as per your question to decide which is better to use to comment the python code, there is my thumb rule that it should comment based on most outer indentation line in selected code lines.

    So your second way is what I will suggest.

    For single line comment there is no need to decide between the indentation of other line of code.