PEP8 是Python官方的编码风格。其源于对于Python良好编码风格的研究,遵循这种风格能够 使得写出来的Python代码更加可读,进而容易维护。
言归正传,开始学习Python的代码风格吧。
代码布局
先来看看正确的代码布局长啥样:
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# More indentation included to distinguish this from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# Hanging indents should add a level.
foo = long_function_name(
var_one, var_two,
var_three, var_four)
这种布局一般的编译器都会支持自动解决,大概知道就好了。
顺手来看一下不良的布局长啥样:
# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Further indentation required as indentation is not distinguishable.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
if语句的排版:
# No extra indentation.
if (this_is_one_thing and
that_is_another_thing):
do_something()
f (this_is_one_thing and
that_is_another_thing):
#Since both conditions are true, we can frobnicate.
do_something()
```
定义列表:
```Python
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
Tab还是空格
用空格就好了。
已经在vim里面把Tab改成空格了,再也不用担心我用错了。
行最大长度
一行最长能写79个字符。
如果一句语句太长了,建议要换行,换行应该在运算符之后进行。
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
```
# 空行
用两行空行来围绕顶级方法和类
在类里面的方法定义用一行空行围绕起来。
# 源文件编码
用UTF-8就好了`# coding=UTF-8`
# 导入
正确的导入姿势是:
```Python
import os
import sys
from subprocess import Popen, PIPE
错误的是:
import sys, os
正确的导入顺序是:
- standard library imports
- related third party imports
- local application/library specific imports
用一行空格区分不同的group
多用绝对地址的导入方式。不要用通配符导入的方式。
正确的在表达式和语句中插入空格
比如说:
Yes: spam(ham[1], {eggs: 2})
No: spam( ham[ 1 ], { eggs: 2 } )
Yes: if x == 4: print x, y; x, y = y, x
No: if x == 4 : print x , y ; x , y = y , x
Yes:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
No:
ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]
```
这方面的风格与其他语言差不多。
运算符方面:
```Python
Yes:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
No:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
```
赋值方面:
```Python
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)
关于冒号和->:
Yes:
def munge(input: AnyStr):
def munge(sep: AnyStr = None):
def munge() -> AnyStr:
def munge(input: AnyStr, sep: AnyStr = None, limit=1000):
写复合表达式:
if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()
注释
注释写英文比较好。
代码风格需要动手练习,并由他人给出意见和建议。所以后面就开始敲代码吧....