Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python编码风格 #86

@qingquan-li

Description

@qingquan-li

大多数语言可以写(或者更明白的说, 格式化 )作几种不同的风格。让你的代码对别人更易读是个好想法,养成良好的编码风格对此很有帮助。

对于 Python,PEP 8 引入了大多数项目遵循的风格指导。
PEP 8 参考:https://www.python.org/dev/peps/pep-0008/https://pep8.org/
此外,还有 Google Python 风格指南


  • Indentation 缩进

    • Use 4 spaces per indentation level.

    • 连续多行时应该使用“元素垂直对齐法”或“悬挂缩进法”。When using a hanging indent(悬挂缩进) the following should be considered; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

      Yes:

      # Aligned with opening delimiter.
      foo = long_function_name(var_one, var_two,
                               var_three, var_four)
      
      # More indentation included to distinguish this from the rest.
      def long_function_name(
              var_one, var_two, var_three,
              var_four):
          print(var_one)
      
      # Hanging indents should add a level.
      foo = long_function_name(
          var_one, var_two,
          var_three, var_four)

      No:

      # Arguments on first line forbidden when not using vertical alignment.
      foo = long_function_name(var_one, var_two,
          var_three, var_four)
      
      # Further indentation required as indentation is not distinguishable.
      def long_function_name(
          var_one, var_two, var_three,
          var_four):
          print(var_one)
    • The 4-space rule is optional for continuation lines.

      # Hanging indents *may* be indented to other than 4 spaces.
      foo = long_function_name(
        var_one, var_two,
        var_three, var_four)
  • 使用 4 空格缩进,而非 TAB: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces

    • sublime Text 3 针对 Python 增加设置 Preferences->Settings-User :

      {
      // 显示空格:
      draw_white_space": "all",
      // 设置tab=4space(针对Python)
      "tab_size": 4,
      "translate_tabs_to_spaces": true,
      }
      
  • 折行以确保其不会超过 79 个字符。这有助于小显示器用户阅读,也可以让大显示器能并排显示几个代码文件

  • 使用空行分隔函数和类,以及函数中的大块代码

  • 可能的话,注释独占一行

  • 使用文档字符串

    • Write docstrings for all public modules, functions, classes, and methods.
    • the """ that ends a multiline docstring should be on a line by itself, e.g.:
      """Return a foobang
      
      Optional plotz says to frobnicate the bizbaz first.
      """
  • 空格:

    • 空格放到操作符两边,以及逗号后面,但是括号里侧不加空格: a = f(1, 2) + g(3, 4)

    • = 用于指示关键字参数或默认参数值时,不要在其两侧使用空格:def foo(x, y=1):

    • 不要用空格来垂直对齐多行间的标记, 因为这会成为维护的负担:

      name = Fatli  # name
      long_name = Fatli_LEE  # name that should not be aligned
      # name = Fatli              # name
      # long_name = Fatli_LEE     # name that should not be aligned
  • 统一函数和类命名:

    • 类名用 Pascal Case 帕斯卡命名法(即大驼峰式命名法 Upper Camel Case ),单词首字母大写,例如:
      class ClassName:
      注:我们常说的驼峰命名法,一般指小驼峰式命名法(Lower Camel Case),即第一个单词以小写字母开始,第二个单字的首字母大写,例如:firstName、lastName 。
    • 函数和方法名用小写加下划线的方式,例如:
      def function_name():
    • 模块名用小写加下划线的方式,例如:
      lower_with_under.py
      
  • 不要使用花哨的编码,如果你的代码的目的是要在国际化环境。Python 的默认情况下,UTF-8,甚至普通的 ASCII 总是工作的最好。同样,也不要使用非 ASCII 字符的标识符,除非是不同语种的会阅读或者维护代码。

    • #!/usr/bin/python :说明脚本语言是 Python:是要用 /usr/bin 目录下面的程序(工具)python,这个解释器来解释 python 脚本
    • # -*- coding: UTF-8 -*- :指定文件编码为 UTF-8
  • 引号:一个纯粹的字符串,使用单引号。一个内部含有需要转义并展开用变量的字符串,用双引号

  • 导入格式:

    • 每个导入应该独占一行

      # Correct:
      import os
      import sys
      # Wrong:
      import os, sys
      
      # It’s okay to say this though:
      from subprocess import Popen, PIPE
    • 导入总应该放在文件顶部,位于模块注释和文档字符串之后,模块全局变量和常量之前。导入应该按照从最通用到最不通用的顺序分组:

      1. 标准库导入
      2. 第三方库导入
      3. 应用程序指定导入
    • 每种分组中,应该根据每个模块的完整包路径按字典序排序,忽略大小写:

      import foo
      from foo import bar
      from foo.bar import baz
      from foo.bar import Quux
      from Foob import ar

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions