首頁  >  文章  >  後端開發  >  八個重構技巧使得Python程式碼更Pythonic

八個重構技巧使得Python程式碼更Pythonic

WBOY
WBOY轉載
2023-04-10 15:01:401827瀏覽

1.合併嵌套的if語句

我們從簡單的開始。不要像這樣嵌套 if 語句,只需將它們合併為一個即可。

if a:
if b:
pass

# -> refactor
if a and b:
pas

2.使用 any 而不是循環

這裡我們要檢查列表中是否至少有一個正元素。更長的解決方案是遍歷所有數字,檢查當前數字,然後在條件為真時中斷。但對於這個任務,在 Python 中有一個專門的方法,就是 any 函數。如果可迭代物件的任何元素為真,則 any 傳回 True。這比手動循環要短得多,也更像 pythonic。

numbers = [-1, -2, -4, 0, 3, -7]
has_positives = False
for n in numbers:
if n > 0:
has_positives = True
break

# -> refactor
has_positives = any(n > 0 for n in numbers)

3.從 for/while 迴圈中提取語句

很多時候你會看到循環中定義了一個變量,但它永遠不會改變。這些都是不必要的操作,所以把它從循環中拉出來,然後你只需要創建一次。

for building in buildings:
city = 'London'
addresses.append(building.street_address, city)

# -> refactor
city = 'London'
for building in buildings:
addresses.append(building.street_address, city)

4.去除只使用一次並立即返回的內聯變量

很多時候你會看到代碼在最後一個函數中定義了一個變量,一行之後它立即返回。如果清楚函數是做什麼的,直接回傳結果即可。這樣更簡潔並且避免了不必要的變數。但是,有時如果不是很清楚函數在做什麼,可以給最後一個變數一個有意義的名稱並將其用作自文檔程式碼。

def state_attributes(self):
"""Return the state attributes."""
state_attr = {
ATTR_CODE_FORMAT: self.code_format,
ATTR_CHANGED_BY: self.changed_by,
}
return state_attr

# -> refactor
def state_attributes(self):
"""Return the state attributes."""
return {
ATTR_CODE_FORMAT: self.code_format,
ATTR_CHANGED_BY: self.changed_by,
}

5.用if表達式取代if語句

不用 if else 語句來設定變數的值,你可以像這樣用 if 表達式在一行中設定它。不過,這種重構技術有點值得商榷。有些人仍然喜歡第一個選項,這很好。

if condition:
x = 1
else:
x = 2

# -> refactor
x = 1 if condition else 2

6.新增警示聲明

查看此程式碼時,很難快速掌握正在發生的事情。有多個 if-else 語句和多個縮排。一旦你仔細觀察,你可能會發現第一個 if 語句幾乎覆蓋了整個函數代碼,只是在最後我們有相應的 else 子句,我們只返回 False。

我們可以把這個 else 子句移到最開始。這也稱為警示聲明。所以如果條件不成立,我們就不會執行其餘的函數程式碼。這樣就去掉了一個 else 子句,現在整個程式碼中的縮排少了一層。這看起來更清晰,也更容易理解。

def should_i_wear_this_hat(self, hat):
if isinstance(hat, Hat):
current_fashion = get_fashion()
weather_outside = self.look_out_of_window()
is_stylish = self.evaluate_style(hat, current_fashion)
if weather_outside.is_raining:
print("Damn.")
return True
else:
print("Great.")
return is_stylish
else:
return False

# -> refactor
def should_i_wear_this_hat(self, hat):
if not isinstance(hat, Hat):
return False

current_fashion = get_fashion()
weather_outside = self.look_out_of_window()
is_stylish = self.evaluate_style(hat, current_fashion)
if weather_outside.is_raining:
print("Damn.")
return True
else:
print("Great.")
return is_stylish

7.將分配移近用法位置

這是上一個範例的改進程式碼,但仍然需要一些時間才能理解這裡發生的事情。所以我們想檢查我們是否應該戴帽子。邏輯是這樣的:如果正在下雨,我們總是說 True,如果沒有下雨,如果帽子很時尚,我們就說 True。我們可以大大提高此邏輯的可讀性的簡單方法是將分配移至更接近其用法的位置。在使用 if 語句之前我們需要先了解天氣狀況。現在 fashion 和 style 變數只在 else 子句中需要,所以將它們向下移動。現在應該更清楚發生了什麼。

前面第 4 條中,我們可以進一步縮短程式碼並立即返回評估樣式結果。而在這個例子中,is_stylish 是個更好名字,因為它告訴你,知道如果帽子很時尚,你就說 True,否則就說 False。所以這裡把多餘的變數留著就好了。

def should_i_wear_this_hat(self, hat):
if not isinstance(hat, Hat):
return False

current_fashion = get_fashion()
weather_outside = self.look_out_of_window()
is_stylish = self.evaluate_style(hat, current_fashion)
if weather_outside.is_raining:
print("Damn.")
return True
else:
print("Great.")
return is_stylish

# -> refactor
def should_i_wear_this_hat(self, hat):
if not isinstance(hat, Hat):
return False

weather_outside = self.look_out_of_window()
if weather_outside.is_raining:
print("Damn.")
return True
else:
print("Great.")
current_fashion = get_fashion()
return self.evaluate_style(hat, current_fashion)
# is_stylish = self.evaluate_style(hat, current_fashion)
# return is_stylish

8.簡化序列檢查

這是我經常看到的另一件事。當你需要檢查集合中是否有元素時,例如在列表中,你不需要寫if len(your_list) > 0. 你可以簡單地說if your_list。這是 pep 8 推薦的方法,也稱為真值測試。這是可能的,因為在 Python 中,空序列和集合的計算結果為 False。所以這可以應用於字串、元組、列表、字典和集合。

if len(list_of_hats) > 0:
hat_to_wear = choose_hat(list_of_hats)

# -> refactor
if list_of_hats:
hat_to_wear = choose_hat(list_of_hats)

以上是八個重構技巧使得Python程式碼更Pythonic的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除