python_风格规范_Google

本文档详细介绍了Python编程的风格规范,包括分号、行长度、括号、缩进等方面的使用规则,以及注释、类、字符串、文件处理等方面的最佳实践。强调了代码的可读性和一致性,如每行不超过80个字符,使用4个空格缩进,避免在行尾使用分号等。同时,推荐使用类型注解来提高代码的可读性和可维护性,并给出了类型注解的使用指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分号

tip
不要在行尾加分号, 也不要用分号将两条命令放在同一行.

行长度

tip
每行不超过80个字符

例外:

  1. 长的导入模块语句
  2. 注释里的URL,路径以及其他的一些长标记
  3. 不便于换行,不包含空格的模块级字符串常量,比如url或者路径
  4. Pylint 禁用注释.(例如:`# pylint: disable=invalid-name)

除非是在 with 语句需要三个以上的上下文管理器的情况下,否则不要使用反斜杠连接行.

Python会将 圆括号, 中括号和花括号中的行隐式的连接起来, 你可以利用这个特点. 如果需要, 你可以在表达式外围增加一对额外的圆括号.

Yes: foo_bar(self, width, height, color='black', design=None, x='foo',
             emphasis=None, highlight=0)

     if (width == 0 and height == 0 and
         color == 'red' and emphasis == 'strong'):    

如果一个文本字符串在一行放不下, 可以使用圆括号来实现隐式行连接:

x = ('This will build a very long long '
     'long long long long long long string')

在注释中,如果必要,将长的URL放在一行上。

Yes:  # See details at
# http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html


No:  # See details at
# http://www.example.com/us/developer/documentation/api/content/\
# v2.0/csv_file_name_extension_full_specification.html     

with 表达式需要使用三个及其以上的上下文管理器时,可以使用反斜杠换行.若只需要两个,请使用嵌套的with.

Yes:  with very_long_first_expression_function() as spam, \
           very_long_second_expression_function() as beans, \
           third_thing() as eggs:
          place_order(eggs, beans, spam, beans)


No:  with VeryLongFirstExpressionFunction() as spam, \
          VeryLongSecondExpressionFunction() as beans:
       PlaceOrder(eggs, beans, spam, beans)


Yes:  with very_long_first_expression_function() as spam:
          with very_long_second_expression_function() as beans:
              place_order(beans, spam)

注意上面例子中的元素缩进; 你可以在本文的 缩进(indentation)部分找到解释.

*另外在其他所有情况下,若一行超过80个字符,但 yapf却无法将该行字数降至80个字符以下时,则允许该行超过80个字符长度.

括号

tip
宁缺毋滥的使用括号

除非是用于实现行连接, 否则不要在返回语句或条件语句中使用括号. 不过在元组两边使用括号是可以的.

Yes: if foo: 
         bar()
       while x:
           x = bar()
       if x and y:
           bar()
       if not x:
           bar()
# For a 1 item tuple the ()s are more visually obvious than the comma.
       onesie = (foo,)
       return foo
       return spam, beans
       return (spam, beans)
       for (x, y) in dict.items(): ...

   
No:  if (x):
         bar()
     if not(x):
         bar()
     return (foo)

缩进

tip
用4个空格来缩进代码

绝对不要用tab, 也不要tab和空格混用. 对于行连接的情况, 你应该要么垂直对齐换行的元素(见 :ref:行长度](line_length* 部分的示例), 或者使用4空格的悬挂式缩进(这时第一行不应该有参数):

Yes:   # Aligned with opening delimiter
      foo = long_function_name(var_one, var_two,
                               var_three, var_four)

       # Aligned with opening delimiter in a dictionary
      foo = {
          long_dictionary_key: value1 +
                               value2,
          ...
      }

       # 4-space hanging indent; nothing on first line
      foo = long_function_name(
          var_one, var_two, var_three,
          var_four)

       # 4-space hanging indent in a dictionary
      foo = {
          long_dictionary_key:
              long_dictionary_value,
          ...
      }
       

No:    # Stuff on first line forbidden
      foo = long_function_name(var_one, var_two,
          var_three, var_four)

       # 2-space hanging indent forbidden
      foo = long_function_name(
        var_one, var_two, var_three,
        var_four)

       # No hanging indent in a dictionary
      foo = {
          long_dictionary_key:
              long_dictionary_value,
              ...
      }

序列元素尾部逗号

tip
仅当 ], ), } 和末位元素不在同一行时,推荐使用序列元素尾部逗号. 当末位元素尾部有逗号时,元素后的逗号可以指示 YAPF将序列格式化为每行一项.

Yes:   golomb3 = [0, 1, 3]
Yes:   golomb4 = [
           0,
           1,
           4,
           6,
       ]


No:    golomb4 = [
           0,
           1,
           4,
           6
       ]

空行

tip
顶级定义之间空两行, 方法定义之间空一行

顶级定义之间空两行, 比如函数或者类定义. 方法定义, 类定义与第一个方法之间, 都应该空一行. 函数或方法中, 某些地方要是你觉得合适, 就空一行.

空格

tip
按照标准的排版规范来使用标点两边的空格

括号内不要有空格.

Yes: spam(ham[1], {eggs: 2}, [])


No:  spam( ham[ 1 ], { eggs: 2 }, [ ] )

不要在逗号, 分号, 冒号前面加空格, 但应该在它们后面加(除了在行尾).

Yes: if x == 4:
         print(x, y)
     x, y = y, x
 

No:  if x == 4 :
         print(x , y)
     x , y = y , x

参数列表, 索引或切片的左括号前不应加空格.

Yes: spam(1)

     
no: spam (1)
  

Yes: dict['key'] = list[index]
  

No:  dict ['key'] = list [index]       

在二元操作符两边都加上一个空格, 比如赋值(=), 比较(==,](, , !=,](,](=, *=, in, not in, is, is not), 布尔(and, or, not). 至于算术操作符两边的空格该如何使用, 需要你自己好好判断. 不过两侧务必要保持一致.

Yes: x == 1


No:  x<1

= 用于指示关键字参数或默认参数值时, 不要在其两侧使用空格. 但若存在类型注释的时候,需要在 = 周围使用空格.

Yes: def complex(real, imag=0.0): return magic(r=real, i=imag)
Yes: def complex(real, imag: float = 0.0): return Magic(r=real, i=imag)



No:  def complex(real, imag = 0.0): return magic(r = real, i = imag)
No:  def complex(real, imag: float=0.0): return Magic(r = real, i = imag)

不要用空格来垂直对齐多行间的标记, 因为这会成为维护的负担(适用于:, #, =等):

Yes:
     foo = 1000  # comment
     long_name = 2  # comment that should not be aligned

     dictionary = {
         "foo": 1,
         "long_name": 2,
         }
  

No:
     foo       = 1000  # comment
     long_name = 2     # comment that should not be aligned

     dictionary = {
         "foo"      : 1,
         "long_name": 2,
         }

Shebang

tip
大部分.py文件不必以#!作为文件的开始. 根据 PEP-394, 程序的main文件应该以 #!/usr/bin/python2 或者 #!/usr/bin/python3 开始.

(译者注: 在计算机科学中, Shebang(也称为Hashbang)是一个由井号和叹号构成的字符串行(#!), 其出现在文本文件的第一行的前两个字符. 在文件中存在Shebang的情

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值