Python indentation in “empty lines”

前端 未结 10 2081
野性不改
野性不改 2020-12-29 18:29

Which is preferred (\".\" indicating whitespace)?

A)

def foo():
    x = 1
    y = 2
....
    if True:
        bar()

B)



        
10条回答
  •  粉色の甜心
    2020-12-29 19:10

    Adding proper indentation to blank lines (style A in the question) vastly improves code readability with display whitespace enabled because it makes it easier to see whether code after a blank line is part of the same indentation block or not.

    For a language like Python, where there is no end statement or close bracket, I'm surprised this is not part of PEP. Editing Python with display whitespace on is strongly recommended, to avoid both trailing whitespace and mixed indentation.

    Compare reading the following:

    A)

    def foo():
    ....x = 1
    ....y = 2
    ....
    ....if True:
    ........bar()
    

    B)

    def foo():
    ....x = 1
    ....y = 2
    
    ....if True:
    ........bar()
    

    In A, it is far clearer that the last two lines are part of foo. This is even more useful at higher indentation levels.

提交回复
热议问题