新聞中心
1.合并嵌套的if語句
我們從簡單的開始。不要像這樣嵌套 if 語句,只需將它們合并為一個即可。

在廬山等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網(wǎng)站設計制作、網(wǎng)站建設 網(wǎng)站設計制作按需定制制作,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,品牌網(wǎng)站建設,營銷型網(wǎng)站建設,成都外貿(mào)網(wǎng)站建設,廬山網(wǎng)站建設費用合理。
if a:
if b:
pass
# -> refactor
if a and b:
pass2.使用 any 而不是循環(huán)
這里我們要檢查列表中是否至少有一個正元素。更長的解決方案是遍歷所有數(shù)字,檢查當前數(shù)字,然后在條件為真時中斷。但是對于這個任務,在 Python 中有一個專門的方法,即 any 函數(shù)。如果可迭代對象的任何元素為真,則 any 返回 True。這比手動循環(huán)要短得多,也更像 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 循環(huán)中提取語句
很多時候你會看到循環(huán)中定義了一個變量,但它永遠不會改變。這些都是不必要的操作,所以把它從循環(huán)中拉出來,然后你只需要創(chuàng)建一次。
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.去除只使用一次并立即返回的內(nèi)聯(lián)變量
很多時候你會看到代碼在最后一個函數(shù)中定義了一個變量,一行之后它立即返回。如果清楚函數(shù)是干什么的,直接返回結果即可。這樣更簡潔并且避免了不必要的變量。但是,有時如果不是很清楚函數(shù)在做什么,它仍然會有幫助,然后您可以給最后一個變量一個有意義的名稱并將其用作自文檔代碼。
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 26.添加保護條款
查看此代碼時,很難快速掌握正在發(fā)生的事情。有多個 if-else 語句和多個縮進。一旦你仔細觀察,你可能會發(fā)現(xiàn)第一個 if 語句幾乎覆蓋了整個函數(shù)代碼,只是在最后我們有相應的 else 子句,我們只返回 False。
我們可以把這個 else 子句移到最開始。這也稱為警衛(wèi)聲明。所以如果條件不成立,我們就不會執(zhí)行其余的函數(shù)代碼。這樣就去掉了一個 else 子句,現(xiàn)在整個代碼中的縮進少了一層。這看起來更清晰,也更容易理解。
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_stylish7.將分配移近它們的用途
這是上一個示例的改進代碼,但仍然需要一些時間才能理解這里發(fā)生的事情。所以我們想檢查我們是否應該戴帽子。邏輯是這樣的:如果正在下雨,我們總是說 True,如果沒有下雨,如果帽子很時尚,我們就說 True。我們可以大大提高此邏輯的可讀性的一種簡單方法是將分配移至更接近其用法的位置。在使用 if 語句之前讓我們先了解天氣情況?,F(xiàn)在 fashion 和 style 變量只在 else 子句中需要,所以將它們向下移動?,F(xiàn)在應該更清楚發(fā)生了什么。
還記得我的第 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_stylish8.簡化序列檢查
這是我經(jīng)??吹降牧硪患隆.斈阈枰獧z查集合中是否有元素時,例如在列表中,你不需要寫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
網(wǎng)站鏈接:http://fisionsoft.com.cn/article/cdeidec.html


咨詢
建站咨詢
