没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
1
Google Python 编程风格指南
原文出处:http://code.google.com/p/zh-google-styleguide/
本文整理:li3p AT yahoo.cn
目录
Google Python 编程风格指南......................................................................................................1
Python 风格规范..............................................................................................................................3
分号................................................................................................................................................3
行长度............................................................................................................................................3
括号................................................................................................................................................3
缩进................................................................................................................................................4
空行................................................................................................................................................4
空格................................................................................................................................................5
Python 解释器..............................................................................................................................6
注释................................................................................................................................................6
类....................................................................................................................................................9
字符串............................................................................................................................................9
TODO 注释..................................................................................................................................10
导入格式......................................................................................................................................11
语句..............................................................................................................................................11
访问控制......................................................................................................................................12
命名..............................................................................................................................................12
Main.............................................................................................................................................13
Python 语言规范............................................................................................................................15
pychecker.................................................................................................................................15
导入..............................................................................................................................................16
包..................................................................................................................................................16
异常..............................................................................................................................................17
全局变量......................................................................................................................................17
2
嵌套/本地/内部类或函数...........................................................................................................18
列表推导(List Comprehensions)............................................................................................18
默认迭代器和操作符..................................................................................................................19
生成器..........................................................................................................................................20
Lambda 函数.............................................................................................................................20
默认参数值..................................................................................................................................21
属性(properties)........................................................................................................................22
True/False 的求值......................................................................................................................23
过时的语言特性..........................................................................................................................24
静态 Scoping(Lexical Scoping).............................................................................................25
函数与方法装饰器......................................................................................................................26
线程..............................................................................................................................................27
威力过大的特性..........................................................................................................................27
3
Python 风格规范
分号
不要在行尾加分号, 也不要用分号将两条命令放在同一行.
行长度
每行不超过 80 个字符
例外: 如果使用 Python 2.4 或更早的版本, 导入模块的行可能多于 80 个字符.
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')
注意上面例子中的元素缩进; 你可以在本文的
缩进
部分找到解释.
括号
宁缺毋滥的使用括号
除非是用于实现行连接, 否则不要在返回语句或条件语句中使用括号. 不过在元组两边使用
括号是可以的.
Yes:
if foo:
bar()
while x:
x = bar()
if x and y:
bar()
if not x:
4
bar()
return foo
for (x, y) in dict.items(): ...
No: if (x):
bar()
if not(x):
bar()
return (foo)
缩进
用 4 个空格来缩进代码
绝对不要用 tab, 也不要 tab 和空格混用. 对于行连接的情况, 你应该要么垂直对齐换行的
元素(见
行长度
部分的示例), 或者使用 4 空格的悬挂式缩进(这时第一行不应该有参数):
Yes: # Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 4-space hanging indent; nothing on first line
foo = long_function_name(
var_one, var_two, var_three,
var_four)
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)
空行
顶级定义之间空两行, 方法定义之间空一行
顶级定义之间空两行, 比如函数或者类定义. 方法定义, 类定义与第一个方法之间, 都应该
空一行. 函数或方法中, 某些地方要是你觉得合适, 就空一行.
5
空格
按照标准的排版规范来使用标点两边的空格
1. 括号内不要有空格.
Yes:
spam(ham[1], {eggs: 2}, [])
No: spam( ham[ 1 ], { eggs: 2 }, [ ] )
2. 不要在逗号, 分号, 冒号前面加空格, 但应该在它们后面加(除了在行尾).
Yes:
if x == 4:
print x, y
x, y = y, x
No
: if x == 4 :
print x , y
x , y = y , x
3. 参数列表, 索引或切片的左括号前不应加空格.
Yes: spam(1)
Yes: spam (1)
Yes: dict['key'] = list[index]
No: dict ['key'] = list [index]
4. 在二元操作符两边都加上一个空格, 比如赋值(=), 比较(==, <, >, !=, <>, <=, >=, in, not
in, is, is not), 布尔(and, or, not). 至于算术操作符两边的空格该如何使用, 需要你自己
好好判断. 不过两侧务必要保持一致.
Yes: x == 1
No: x<1
5. 当’=’用于指示关键字参数或默认参数值时, 不要在其两侧使用空格.
Yes: def complex(real, imag=0.0): return magic(r=real, i=imag)
No
: def complex(real, imag = 0.0): return magic(r = real, i = imag)
6. 不要用空格来垂直对齐多行间的标记, 因为这会成为维护的负担(适用于:, #, =等):
Yes:
foo = 1000 # comment
long_name = 2 # comment that should not be aligned
dictionary = {
剩余26页未读,继续阅读
li3p
- 粉丝: 1
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页