Python的基本編碼規範

2021-09-11 06:03:34 字數 4730 閱讀 7060

python enhancement proposals :python改進方案

pep8 官網規範位址

pep-0008/

風格指南強調一致性。專案、模組或函式保持一致都很重要。

每級縮排用4個空格。

括號中使用垂直隱式縮排或使用懸掛縮排。後者應該注意第一行要沒有引數,後續行要有縮排。

# 對準左括號

foo = long_function_name(var_one, var_two,

var_three, var_four)

# 不對準左括號,但加多一層縮排,以和後面內容區別。

def long_function_name(

var_one, var_two, var_three,

var_four):

print(var_one)

# 懸掛縮排必須加多一層縮排.

foo = long_function_name(

var_one, var_two,

var_three, var_four)

# 不使用垂直對齊時,第一行不能有引數。

foo = long_function_name(var_one, var_two,

var_three, var_four)

# 引數的縮排和後續內容縮排不能區別。

def long_function_name(

var_one, var_two, var_three,

var_four):

print(var_one)

4個空格的規則是對續行可選的。

# 懸掛縮排不一定是4個空格

foo = long_function_name(

var_one, var_two,

var_three, var_four)

if語句跨行時,兩個字元關鍵字(比如if)加上乙個空格,再加上左括號構成了很好的縮排。後續行暫時沒有規定,至少有如下三種格式,建議使用第3種。

# 沒有額外縮排,不是很好看,個人不推薦.

if (this_is_one_thing and

that_is_another_thing):

do_something()

# 新增注釋

if (this_is_one_thing and

that_is_another_thing):

# since both conditions are true, we can frobnicate.

do_something()

# 額外新增縮排,推薦。

# add some extra indentation on the conditional continuation line.

if (this_is_one_thing

and that_is_another_thing):

do_something()

右邊括號也可以另起一行。有兩種格式,建議第2種。

# 右括號不回退,個人不推薦

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?

最大行寬

空行原始檔編碼

匯入在單獨行

禁止使用萬用字元匯入。

萬用字元匯入(from import *)應該避免,因為它不清楚命名空間有哪些名稱存,混淆讀者和許多自動化的工具。

字串引用

括號裡邊避免空格

# 括號裡邊避免空格

# yes

spam(ham[1], )

# no

spam( ham[ 1 ], )

逗號,冒號,分號之前避免空格

# 逗號,冒號,分號之前避免空格

# 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]

函式呼叫的左括號之前不能有空格

# yes

spam(1)

dct['key'] = lst[index]

# no

spam (1)

dct ['key'] = lst [index]

賦值等操作符前後不能因為對齊而新增多個空格

# yes

x = 1

y = 2

long_variable = 3

# no

x = 1

y = 2

long_variable = 3

二元運算子兩邊放置乙個空格

涉及 =、符合操作符 ( += , -=等)、比較( == , < , > , != , <> , <= , >= , in , not in , is , is not )、布林( and , or , not )。

優先順序高的運算子或操作符的前後不建議有空格。

# 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)

關鍵字引數和預設值引數的前後不要加空格

# 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)

通常不推薦復合語句(compound statements: 多條語句寫在同一行)。

# yes

if foo == 'blah':

do_blah_thing()

do_one()

do_two()

do_three()

# no

if foo == 'blah': do_blah_thing()

do_one(); do_two(); do_three()

儘管有時可以在if/for/while 的同一行跟一小段**,但絕不要跟多個子句,並盡量避免換行。

# no

if foo == 'blah': do_blah_thing()

for x in lst: total += x

while t < 10: t = delay()

更不是:

# no

if foo == 'blah': do_blah_thing()

else: do_non_blah_thing()

try: something()

finally: cleanup()

do_one(); do_two(); do_three(long, argument,

list, like, this)

if foo == 'blah': one(); two(); three()

避免採用的名字

決不要用字元'l'(小寫字母el),'o'(大寫字母oh),或 'i'(大寫字母eye) 作為單個字元的變數名。一些字型中,這些字元不能與數字1和0區別。用'l' 代替'l'時。

包和模組名

模組名要簡短,全部用小寫字母,可使用下劃線以提高可讀性。包名和模組名類似,但不推薦使用下劃線。

Python基本編碼規範

1 分號 不得在行尾加分號,也不要使用分號將兩條命令放在同一行。python有很嚴格的語法規範,在其他一些程式語言中使用分號可以實現你想要的隔離功能,但在python不適用。2 行長度 一般情況下每行不要超過80個字元,超出時可以使用換行進行處理,各類括號會將內容連線在一起。3 括號 能不用就不用,...

Python編碼規範

usr bin python 應用程式要定義main函式而不要直接寫指令碼 def main dosomething if name main main 如果是全域性變數,則需要顯式加上 global python 的注釋分為兩種的概念,一種是由 開頭的 真正的 注釋,另一種是 docstrings...

Python編碼規範

usr bin python 應用程式要定義main函式而不要直接寫指令碼 def main dosomething if name main main 如果是全域性變數,則需要顯式加上 global python 的注釋分為兩種的概念,一種是由 開頭的 真正的 注釋,另一種是 docstrings...