Python 的代码风格

来源:互联网 发布:阿玛尼手表淘宝 编辑:程序博客网 时间:2024/04/28 12:44

正在看Python3,看到doc里面有一段关于代码风格的说明,摘抄过来大致翻译了下

  • Use 4-space indentation, and no tabs.
    使用4个空格键来实现缩进,不要使用TAB键

    4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.

  • Wrap lines so that they don’t exceed 79 characters.
    使用换行控制每行不超过79个字符

    This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.

  • Use blank lines to separate functions and classes, and larger blocks of code inside functions.
    在函数、类以及函数中的大段代码中插入空白行进行隔离

  • When possible, put comments on a line of their own.
    如果可能的话,注释写在对应的行上

  • Use docstrings.
    使用DocString 也就是‘’‘或者“”“包含部分

  • Use spaces around operators and after commas, but not directly inside bracketing constructs:a =f(1, 2) +g(3, 4).
    在运算符和逗号周边加上空格键,但是不能破坏结构

  • Name your classes and functions consistently; the convention is to useCamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).
    大写单词首字母作为类的名称,小写+下划线为函数和方法命名,总是方法的第一个参数名称使用self

  • Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.
    缺省UTF-8,甚至是ASCII作为文件编码

  • Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.
    不用使用非ASCII编码的标志符


0 0