在 python 中换行(使用反斜杠或括号)时缩进有什么重要性吗?

问题描述 投票:0回答:1

在 python 中换行(使用反斜杠或括号)时缩进是否重要?款式推荐推荐哪一款呢?我试着问 bing chat/chatGPT,它认为在断行后缩进是必须的。 例如这个代码块:

def print_empty_square(size, c):
    for row in range(size):
        for col in range(size):
            if (row == 0 or row == size -1 
or col == 0 or col == size -1):
                   print(c,end = "")
            else:
                print(' ',end = "")
        print()
def print_empty_square(size, c):
    for row in range(size):
        for col in range(size):
            if (row == 0 or row == size -1 
             or col == 0 or col == size -1):
                   print(c,end = "")
            else:
                print(' ',end = "")
        print()

两种代码我都试过了,两种代码都运行成功。 我问了 bing chat 和 chatgpt 这是他们的答案:

是的,缩进在 Python 中非常重要。 Python 使用缩进来指示代码块。在其他编程语言中,代码中的缩进只是为了可读性,而 Python 中的缩进非常重要。 Python 将具有相同缩进级别的语句(在它们之前具有相同数量的空格的语句)视为单个代码块¹³。

在您的例子中,两个代码都成功运行,因为 Python 在运行代码时会忽略行首的空行和空格²。

来源:Conversation with Bing,02/04/2023(1) Python Indentation - W3Schools。 https://www.w3schools.com/python/gloss_python_indentation.asp 2023 年 2 月 4 日访问。 (2) Indentation in Python - GeeksforGeeks。 https://www.geeksforgeeks.org/indentation-in-python/ 2023 年 2 月 4 日访问。 (3) Python 中的缩进 | Python 缩进 - 缩放器主题。 https://www.scaler.com/topics/python/indentation-in-python/ 2023 年 2 月 4 日访问。 (4) 为什么缩进在 Python 中很重要 - tutorialspoint.com。 https://www.tutorialspoint.com/Why-is-indentation-important-in-Python 2023 年 2 月 4 日访问。

python python-3.x indentation
1个回答
1
投票

视情况而定。正如您已经成功测试的那样,缩进在 Python 中可能有意义,也可能没有意义。

代码块缩进

一般规则是属于一个代码块的所有语句必须缩进相同数量的空格。最低级别不得缩进。

def print_some_stuff(n_times, extra_line):
    print('This is one level of indentation')
    for i in range(n_times):
        print(i + 1)
        print('This is another one')
    if extra_line:
        print('This is one as well')

print('This is the lowest level (zero indentation)')
print_some_stuff(5, True)

实际的空白数量在这里没有固定,即是否将代码块的所有语句缩进 1、4 或 20 个空格在有效性方面没有区别。对于两个不同代码块的不同数量的空白也是如此。理论上,一个块可以缩进 1 个空格,下一个块可以缩进 3 个空格,只要缩进within 每个块是一致的。

缩进声明中

您在问题中描述的不是代码块中不同语句的缩进,而是单个语句中的缩进。语句可以 隐式 如果它们被包裹在括号中,或者 显式 如果每行(当然最后一行除外)以单个反斜杠结束(

\
)。在这里,连续行之前有多少空格并不重要。这就是你观察到的。

以下内容虽然丑陋且难以阅读,但完全有效:

def print_some_stuff(n_times, 
extra_line):
    print('This is one level of indentation')
    for i \
in range(n_times):
        print(i 
              + 1)
        print('This is another one')
    if \
    extra_line:
        print('This is one as well')

print('This is the lowest level (zero indentation)')
print_some_stuff(5, 
                 True)

样式 (PEP-8)

虽然如上所述,不同数量的空格在不同情况下可能有效,但PEP8 - Python代码风格指南特别是它关于缩进的部分清楚地定义了在哪种情况下使用多少空格。

我强烈建议您阅读整篇文章,但简而言之:

  • 每个缩进级别使用 4 个空格
  • 空格是首选的缩进方式
  • 隐式延续(在括号内)通常优于显式(使用反斜杠)
  • 续行应与开始分隔符垂直对齐或使用 悬挂缩进 和额外级别的缩进进行格式化
    • 与开始分隔符对齐
      print_some_stuff(5, 
                       True)
      
    • 使用悬挂缩进时,将第一个参数换行
      def print_some_stuff(
               n_times, 
               extra_line):
          print('This is one level of indentation') 
      
      print_some_stuff(
          5, 
          True)
      

PEP-8 中还有更多内容,例如有关断行时将二元运算符放在哪里的详细信息等,所以请继续查看。这些东西确实有据可查,所以如果我是你,我会去那里而不是问 ChatGPT。

© www.soinside.com 2019 - 2024. All rights reserved.