Pocoo 风格指南
所有 Pocoo 项目都遵循 Pocoo 风格指南, Flask 项目也不例外。 Flask 补丁必须遵循这个指南,同时也推荐 Flask 扩展遵循这个指南。
一般而言, Pocoo 风格指南遵循 PEP 8 ,有一些小差异和扩充。
总体布局
- 缩进:
- 4个空格。不使用制表符,没有例外。
- 最大行长:
- 软限制为 79 个字符,不超过 84 个字符。尝试合理放置 break 、 continue和 return 声明来避免代码过度嵌套。
- 续行:
- 可以使用反斜杠来续行,续行应对齐最后一个点号或等于号,或者缩进四个空格:
1. this_is_a_very_long(function_call, 'with many parameters') \
2. .that_returns_an_object_with_an_attribute
4. MyModel.query.filter(MyModel.scalar > 120) \
5. .order_by(MyModel.name.desc()) \
6. .limit(10)
如果你在括号内的换行,那么续行应对齐括号:
1. this_is_a_very_long(function_call, 'with many parameters',
2. 23, 42, 'and even more')
对于有许多元素的元组或列表,在起始括号后立即换行:
1. items = [
2. 'this is the first', 'set of items', 'with more items',
3. 'to come in this line', 'like this'
4. ]
- 空行:
- 顶层函数和类由两个空行分隔,其它一个空行。不要使用过多空行来分隔代码逻辑段。例如:
1. def hello(name):
2. print 'Hello %s!' % name
5. def goodbye(name):
6. print 'See you %s.' % name
9. class MyClass(object):
10. """This is a simple docstring"""
12. def __init__(self, name):
13. self.name = name
15. def get_annoying_name(self):
16. return self.name.upper() + '!!!!111'
表达式和语句
- 常规空格规则:
-
- 不是单词的一元运算符不使用空格(例如:
-、~等等),在圆括号也是这样。
- 不是单词的一元运算符不使用空格(例如:
- 用空格包围二元运算符。
对:
1. exp = -1.05
2. value = (item_value / item_count) * offset / exp
3. value = my_list[index]
4. value = my_dict['key']
错:
1. exp = - 1.05
2. value = ( item_value / item_count ) * offset / exp
3. value = (item_value/item_count)*offset/exp
4. value=( item_value/item_count ) * offset/exp
5. value = my_list[ index ]
6. value = my_dict ['key']
- 禁止 Yoda 语句:
- 永远不要用变量来比较常量,而是用常量来比较变量:
对:
1. if method == 'md5':
2. pass
错:
1. if 'md5' == method:
2. pass
- 比较:
-
- 针对任意类型使用
==和!=
- 针对任意类型使用
- 针对单一类型使用
is和is not(例如:foo is not None) - 永远不要与
True或False作比较(例如永远不要写foo == False,而应当写not foo) - 排除检验:
- 使用
foo not in bar而不是not foo in bar - 实例检验:
- 使用
isinstance(a, C)而不是type(A) is C,但是通常应当避免检验实例,而应当检验特性。
命名约定
- 类名:
CamelCase,缩写词大写(HTTPWriter而不是HttpWriter) - 变量名:
lowercase_with_underscores - 方法和函数名:
lowercase_with_underscores - 常量:
UPPERCASE_WITH_UNDERSCORES - 预编译正则表达式:
name_re
被保护的成员以单个下划线作为前缀,混合类则使用双下划线。
如果使用关键字作为类的名称,那么在名称末尾添加下划线。与内置构件冲突是允许的,请 一定不要 用在变量名后添加下划线的方式解决冲突。如果函数需要访问一个隐蔽的内置构件,请重新绑定内置构件到一个不同的名字。
- 函数和方法参数:
-
- 类方法:
cls作为第一个参数
- 类方法:
- 实例方法:
self作为第一个参数 - 用于属性的 lambda 表达式应该把第一个参数替换为
x,像display_name = property(lambda x: x.real_name or x.username)中一样
文档字符串
- 文档字符串约定:
- 所有的文档字符串为 Sphinx 可理解的 reStructuredText 格式。它们的形态因行数不同而不同。如果只有一行,三引号闭合在同一行,否则开头的三引号与文本在同一行,结尾的三引号独立一行:
1. def foo():
2. """This is a simple docstring"""
5. def bar():
6. """This is a longer docstring with so much information in there
7. that it spans three lines. In this case the closing triple quote
8. is on its own line.
9. """
- 模块头:
- 模块头包含一个 utf-8 编码声明(即使没有使用非 ASCII 字符,也始终推荐这么做)和一个标准的文档字符串:
1. # -*- coding: utf-8 -*-
2. """
3. package.module
4. ~~~~~~~~~~~~~~
6. A brief description goes here.
8. :copyright: (c) YEAR by AUTHOR.
9. :license: LICENSE_NAME, see LICENSE_FILE for more details.
10. """
谨记使用合适的版权和许可证文件以利于通过 Flask 扩展审核。
注释
注释的规则与文档字符串类似。两者都使用 reStructuredText 格式。如果一个注释被用于一个说明类属性,在起始的井号( # )后加一个冒号:
1. class User(object):
2. #: the name of the user as unicode string
3. name = Column(String)
4. #: the sha1 hash of the password + inline salt
5. pw_hash = Column(String)
