[Python基礎]寫程式碼的風格指南 PEP8

2024/01/23閱讀時間約 8 分鐘

在人與人溝通之間,最怕雞同鴨講,彼此不對頻的狀況常會造成誤會。在程式語言中也會出現類似的情況,所以就有一些約定來彼此約束。

PEP 8 是 Python 社群廣泛遵循的一種風格指南,用於提高 Python 程式碼的可讀性和一致性。一開始是 Python 之父 Guido van Rossum 自己的撰碼風格,慢慢後來演變至今。

使用一致的風格,可以讓人更容易讀取你的程式碼也易維護,也能讓專案的協作進行的更加順利。

列出以下比較常用的規則:

檔案編碼: 使用 UTF-8 作為檔案編碼。

縮排:

  • 使用 4 個空格進行縮排。
# Good
def example_function():
pass

# Bad
def example_function():
pass

空格和運算符:

在運算符周圍放置一個空格,但不要在括號內做這樣的事情。

# Good
result = x + y

# Bad
result = x+y

在以下情況下避免使用多餘的空格:

緊鄰圓括號、方括號或大括號內:

# Good
spam(ham[1], {eggs: 2})

# bad
spam( ham[ 1 ], { eggs: 2 } )

緊接在逗號、分號或冒號之前:

# Good
if x == 4: print(x, y); x, y = y, x

# bad
if x == 4 : print(x , y) ; x , y = y , x

如果使用具有不同優先權的運算符,請考慮在優先權最低的運算子周圍新增空格:

# Good

i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)

# bad

i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)

行長度:

  • 一行的長度應該不超過 79 個字符,可以使用反斜杠 \ 進行行連接。
  • 如果一行過長,可以在括號內的表達式、花括號內的字典、方括號內的列表等地方進行換行。
  • 使用反斜杠進行行連接:
long_line = "This is a very long line that exceeds the recommended length " \
"of 79 characters. Using a backslash allows us to break it " \
"into multiple lines for better readability."

在括號內進行換行:

result = some_function_that_takes_arguments(
'argument1', 'argument2', 'argument3',
'argument4', 'argument5', 'argument6'
)

在字典(花括號內)進行換行:

my_dict = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
}

在列表(方括號內)進行換行:

my_list = [
'item1', 'item2', 'item3',
'item4', 'item5', 'item6',
]

二元運算子的換行位置

# Good
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)

# bad
income = (gross_wages +
taxable_interest +
(dividends - qualified_dividends) -
ira_deduction -
student_loan_interest)

空行:

  • 在不同函式或類之間應當有兩行空行。
  • 在函式或類內部,根據邏輯結構添加適當的空行以提高可讀性。
# Good
def function1():
pass

def function2():
pass

class ExampleClass:
def __init__(self):
pass

# Bad
def function1():
pass
def function2():
pass

class ExampleClass:
def __init__(self):
pass

import:

  • 單獨導入應當放在文件頂部,並按照標準順序進行導入。
  • 避免使用通用導入 import *
  • 每行 import 只導入一個模組
  • 每個分組之間用一行空行分隔 1. 標準庫 2. 相關第三方庫 3. 自己的模組
  • 推薦使用絕對導入
# Good
import module1
import module2

# Bad
from module1 import *

#推薦使用絕對導入
import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example

命名規則:

  • 函式、變數和屬性使用小寫字母,單詞之間使用底線 _ 分隔(snake_case)。
  • 類使用大寫字母開頭的單詞(CapWords,或稱 CamelCase)。
  • 切勿使用字元“l”(小寫字母 el)、“O”(大寫字母 oh)或“I”(大寫字母 eye)作為單字元變數名稱。

在某些字體中,這些字元與數字 1 和 0 無法區分。想要使用“l”時,請改用“L”

# Good
def example_function():
pass

class ExampleClass:
pass

# Bad
def ExampleFunction():
pass

class example_class:
pass

與None的比較,用is, not is。不要用 == 與!=。

# Good:
variable = None

if variable is None:
print("變數是 None")
else:
print("變數不是 None")

# Bad:​
if variable == None:
print("變數是 None")
else:
print("變數不是 None")

另外 is not順序也很重要。雖然這兩個表達式在功能上相同,但前者更具可讀性

# Good:
if foo is not None:

# Bad:
if not foo is None:

檢查序列(字串、列表、元組)是否為空值時

使用 if not seqif seq 更加 Pythonic,更符合慣例,並提高了代碼的可讀性避免不必要的函數調用,使代碼更簡潔。

# Good
if not seq:
if seq:

# Bad
if len(seq):
if not len(seq):


程式撰寫方式可以因語言、專案需求、團隊風格等因素而有所不同。大家互相看的順眼最重要。



49會員
88內容數
Python程式設計師,不是在學習就是在學習的路上
留言0
查看全部
發表第一個留言支持創作者!