设为首页 加入收藏

TOP

Python学习笔记 --- Python 编码规范
2018-12-13 18:28:23 】 浏览:30
Tags:Python 学习 笔记 --- 编码 规范
版权声明:学习交流为主,未经博主同意禁止转载,禁止用于商用。 https://blog.csdn.net/u012965373/article/details/77932554

Python is the main scripting used at Google.

1.行为不加分号,每行一条语句。


2.每行不超过80字符:
推荐: 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’):


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

Yes: if foo:
bar()
while x:
x = bar()
if x and y:
bar()
if not x:
bar()
return foo
for (x, y) in dict.items():


4.缩紧:
用4歌空格来缩紧代码


5.空行:
顶级定义之间空两行,方法定义之间空一行
顶级定义之间空两行,比如函数或者类定义。
方法定义,类定义与第一个方法之间,都应该空一行


6.空格
按照标准的排版规则是用标点两边的空格,括号内不要有空格。
不要在逗号,分号,冒号前面加空格,但应该在它们后面加(除行尾)。
参数列表,索引或者切片的做括号前面不应该加空格。
在二元操作符两边都加上一个空格,比如复制(=),比较等。
Yes: x == 1

当'=‘用于指示关键字参数活着默认参数时,不要在其两侧使用空格。

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

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

Yes:
foo = 1000 # 注释
long_name = 2 # 注释不需要对齐

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




7.大部分.py文件不必以#!作为文件的开始。


8.注释

文档字符串
’’’
’’’

模块

函数和方法

外部不可见
非常短小
简单明了

块注释和行注释
# We use a weighted dictionary search to find out where i is in
# the array. We extrapolate position based on the largest num
# in the array and the array size and then do binary search to
# get the exact number.

if i & (i-1) == 0: # true iff i is a power of 2


为了提高代码,注释应该至少离开代码两个空格,不要描述代码。


9.类
如果一个类不继承自其他累,就显式的从object继承。

Yes: class SampleClass(object):
pass


class OuterClass(object):

class InnerClass(object):
pass

继承自object 是为了使属性(properties)正常工作,并且这样可以保护你的代码。


10.字符串
避免在循环中用+和+=操作符来累加字符串。
这样做会创建不必要的临时对象。


Yes: x = a + b
x = '%s, %s!' % (imperative, expletive)
x = '{}, {}!'.format(imperative, expletive)
x = 'name: %s; score: %d' % (name, n)
x = 'name: {}; score: {}'.format(name, n)



Yes: items = ['<table>']
for last_name, first_name in employee_list:
items.append('<tr><td>%s, %s</td></tr>' % (last_name, first_name))
items.append('</table>')
employee_table = ''.join(items)

在同一个文件中,保持使用字符串引号的一致性。
使用单引号或者双引号之一,用以引用字符串,并在同一文件中沿用。
Yes:
Python('Why are you hiding your eyes')
Gollum("I'm scared of lint errors.")
Narrator('"Good!" thought a happy Python reviewer.’)



11.文件和sockets
在文件和sockets结束时,显示的关闭它。
推荐使用with 语句。

with open("hello.txt") as hello_file:
for line in hello_file:
print line

对于不支持使用with语句的类似文件的对象,使用context lib。closeing():

import contextlib

with contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page:
for line in front_page:
print line



12.TODO注释
为临时代码使用TODO注释, 它是一种短期解决方案. 不算完美, 但够好了.
TODO注释应该在所有开头处包含"TODO"字符串, 紧跟着是用括号括起来的你的名字, email地址或其它标识符. 然后是一个可选的冒号. 接着必须有一行注释, 解释要做什么. 主要目的是为了有一个统一的TODO格式, 这样添加注释的人就可以搜索到(并可以按需提供更多细节). 写了TODO注释并不保证写的人会亲自解决问题. 当你写了一个TODO, 请注上你的名字.

# TODO(kl@gmail.com): Use a "*" here for string repetition.
# TODO(Zeke) Change this to use relations.



13.导入格式:

每个导入应独占一行

Yes: import os
import sys

导入总应该放在文件顶部, 位于模块注释和文档字符串之后, 模块全局变量和常量之前. 导入应该按照从最通用到最不通用的顺序分组:
标准库导入
第三方库导入
应用程序指定导入
每种分组中, 应该根据每个模块的完整包路径按字典序排序, 忽略大小写.

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


14.语句
每个语句独占一行

Yes:

if foo: bar(foo)


15.命名:

module_name,
package_name,
ClassName,
method_name,
ExceptionName,
function_name,
GLOBAL_VAR_NAME,
instance_var_name,
function_parameter_name,
local_var_name.


16.Main
If __name__ == ‘__mian__’:
pass








】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇基本数据str的操作 下一篇python的一些练习地址

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目