我們可以把這個(gè) else 子句移到最開始。這也稱為警示聲明。所以如果條件不成立,我們就不會(huì)執(zhí)行其余的函數(shù)代碼。這樣就去掉了一個(gè) else 子句,現(xiàn)在整個(gè)代碼中的縮進(jìn)少了一層。這看起來更清晰,也更容易理解。
 1.合并嵌套的if語句
我們從簡單的開始。不要像這樣嵌套 if 語句,只需將它們合并為一個(gè)即可。
if a:
    if b:
        pass
# -> refactor
if a and b:
    pas
2.使用 any 而不是循環(huán)
這里我們要檢查列表中是否至少有一個(gè)正元素。更長的解決方案是遍歷所有數(shù)字,檢查當(dāng)前數(shù)字,然后在條件為真時(shí)中斷。但是對于這個(gè)任務(wù),在 Python 中有一個(gè)專門的方法,即 any? 函數(shù)。如果可迭代對象的任何元素為真,則 any 返回 True。這比手動(dòng)循環(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)中提取語句
很多時(shí)候你會(huì)看到循環(huán)中定義了一個(gè)變量,但它永遠(yuǎn)不會(huì)改變。這些都是不必要的操作,所以把它從循環(huán)中拉出來,然后你只需要?jiǎng)?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í)候你會(huì)看到代碼在最后一個(gè)函數(shù)中定義了一個(gè)變量,一行之后它立即返回。如果清楚函數(shù)是干什么的,直接返回結(jié)果即可。這樣更簡潔并且避免了不必要的變量。但是,有時(shí)如果不是很清楚函數(shù)在做什么,可以給最后一個(gè)變量一個(gè)有意義的名稱并將其用作自文檔代碼。
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表達(dá)式替換if語句
不用 if else? 語句來設(shè)置變量的值,你可以像這樣用 if 表達(dá)式在一行中設(shè)置它。不過,這種重構(gòu)技術(shù)有點(diǎn)值得商榷。有些人仍然喜歡第一個(gè)選項(xiàng),這很好。
if condition:
    x = 1
else:
    x = 2
# -> refactor
x = 1 if condition else 2
6.添加警示聲明
查看此代碼時(shí),很難快速掌握正在發(fā)生的事情。有多個(gè) if-else 語句和多個(gè)縮進(jìn)。一旦你仔細(xì)觀察,你可能會(huì)發(fā)現(xiàn)第一個(gè) if 語句幾乎覆蓋了整個(gè)函數(shù)代碼,只是在最后我們有相應(yīng)的 else 子句,我們只返回 False。
我們可以把這個(gè) else 子句移到最開始。這也稱為警示聲明。所以如果條件不成立,我們就不會(huì)執(zhí)行其余的函數(shù)代碼。這樣就去掉了一個(gè) else 子句,現(xiàn)在整個(gè)代碼中的縮進(jì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_stylish
7.將分配移近用法位置
這是上一個(gè)示例的改進(jìn)代碼,但仍然需要一些時(shí)間才能理解這里發(fā)生的事情。所以我們想檢查我們是否應(yīng)該戴帽子。邏輯是這樣的:如果正在下雨,我們總是說 True?,如果沒有下雨,如果帽子很時(shí)尚,我們就說 True?。我們可以大大提高此邏輯的可讀性的一種簡單方法是將分配移至更接近其用法的位置。在使用 if? 語句之前我們需要先了解天氣情況。現(xiàn)在 fashion? 和 style? 變量只在 else 子句中需要,所以將它們向下移動(dòng)?,F(xiàn)在應(yīng)該更清楚發(fā)生了什么。
前面第 4 條中,我們可以進(jìn)一步縮短代碼并立即返回評估樣式結(jié)果。而在這個(gè)例子中,is_stylish? 是個(gè)更好名字,因?yàn)樗嬖V你,知道如果帽子很時(shí)尚,你就說 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.簡化序列檢查
這是我經(jīng)??吹降牧硪患隆.?dāng)你需要檢查集合中是否有元素時(shí),例如在列表中,你不需要寫if len(your_list) > 0?. 你可以簡單地說if your_list。這是 pep 8 推薦的方法,也稱為真值測試。這是可能的,因?yàn)樵?Python 中,空序列和集合的計(jì)算結(jié)果為 False。所以這可以應(yīng)用于字符串、元組、列表、字典和集合。
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)