您的位置:首页 > 编程语言 > Python开发

Python PEP8 PEP257 编码规范中文版 学习笔记

2018-12-15 15:33 363 查看

原文链接:http://legacy.python.org/dev/peps/pep-0008/

A Foolish Consistency is the Hobgoblin of little Minds.
尽信书不如无书

与PEP8一致的目录

PEP8

1.缩进用4个空格,尽量不用TAB键
2.行最大字符79,注释字符最大72
3.推荐在二元操作符之前换行

# 推荐:运算符和操作数很容易进行匹配
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)

4.import导入通常在分开的行,

# 推荐这样
import os
import sys
# 不推荐:
import sys, os
# 但是可以这样:
from subprocess import Popen, PIPE

5.程序顶部顺序

# 1.模块注释和文档字符串
"""
模块注释和文档字符串在最上面
"""
# 1.1 __future__这个特例
from __future__ import barry_as_FLUFL

# 2 模块级的呆名
__author__ = 'junqiao'
__version__ = '0.1'

# 3.1标准库的导入
# 3.2相关第三方库的导入
# 3.3本地库的导入
import os
import operate

import numpy
import xadmin

import mylibrary

# 4 变量的定义
myList = []

6.python中单引号和双引号等价
7.避免在尾部添加空格
8.考虑在二元运算符两边加空格,例如 = , + , - , += , ==, and
但是在使用不同优先级的运算符时,考虑在最低优先级的运算符周围添加空格

# 推荐:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)5
#不推荐:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)

在制定关键字参数或者默认参数值时,不要在=附近加上空格

#推荐:
def complex(real, imag=0.0):
return magic(r=real, i=imag)
#不推荐:
def complex(real, imag = 0.0):
return magic(r = real, i = imag)

9.插曲 函数参数注解

# 参数x y return 为int型,但只是注释,
#如果用错,定义时不报错,执行时才会报错
def accumlate(x:int, y:int) -> int:
return x*y

PEP257 规范书写文档

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: