日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

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

來源: 責編: 時間:2023-09-28 10:08:49 682觀看
導讀1.合并嵌套的if語句我們從簡單的開始。不要像這樣嵌套 if 語句,只需將它們合并為一個即可。if a: if b: pass# -> refactorif a and b: pass2.使用 any 而不是循環這里我們要檢查列表中是否至少有一個正

1.合并嵌套的if語句

我們從簡單的開始。不要像這樣嵌套 if 語句,只需將它們合并為一個即可。2zS28資訊網——每日最新資訊28at.com

if a:    if b:        pass# -> refactorif a and b:    pass

2.使用 any 而不是循環

這里我們要檢查列表中是否至少有一個正元素。更長的解決方案是遍歷所有數字,檢查當前數字,然后在條件為真時中斷。但是對于這個任務,在 Python 中有一個專門的方法,即 any 函數。如果可迭代對象的任何元素為真,則 any 返回 True。這比手動循環要短得多,也更像 pythonic。2zS28資訊網——每日最新資訊28at.com

numbers = [-1, -2, -4, 0, 3, -7]has_positives = Falsefor n in numbers:    if n > 0:        has_positives = True        break# -> refactorhas_positives = any(n > 0 for n in numbers)

3.從 for/while 循環中提取語句

很多時候你會看到循環中定義了一個變量,但它永遠不會改變。這些都是不必要的操作,所以把它從循環中拉出來,然后你只需要創建一次。2zS28資訊網——每日最新資訊28at.com

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

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

很多時候你會看到代碼在最后一個函數中定義了一個變量,一行之后它立即返回。如果清楚函數是干什么的,直接返回結果即可。這樣更簡潔并且避免了不必要的變量。但是,有時如果不是很清楚函數在做什么,它仍然會有幫助,然后您可以給最后一個變量一個有意義的名稱并將其用作自文檔代碼。2zS28資訊網——每日最新資訊28at.com

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# -> refactordef 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 表達式在一行中設置它。不過,這種重構技術有點值得商榷。有些人仍然喜歡第一個選項,這很好。2zS28資訊網——每日最新資訊28at.com

if condition:    x = 1else:    x = 2# -> refactorx = 1 if condition else 2

6.添加保護條款

查看此代碼時,很難快速掌握正在發生的事情。有多個 if-else 語句和多個縮進。一旦你仔細觀察,你可能會發現第一個 if 語句幾乎覆蓋了整個函數代碼,只是在最后我們有相應的 else 子句,我們只返回 False。2zS28資訊網——每日最新資訊28at.com

我們可以把這個 else 子句移到最開始。這也稱為警衛聲明。所以如果條件不成立,我們就不會執行其余的函數代碼。這樣就去掉了一個 else 子句,現在整個代碼中的縮進少了一層。這看起來更清晰,也更容易理解。2zS28資訊網——每日最新資訊28at.com

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# -> refactordef 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 子句中需要,所以將它們向下移動。現在應該更清楚發生了什么。2zS28資訊網——每日最新資訊28at.com

還記得我的第 4 條提示嗎?我們可以進一步縮短代碼并立即返回評估樣式結果。然而,在這個例子中,我也喜歡 is_stylish 這個名字,因為它讓你知道如果帽子很時尚,你就說 True,否則就說 False。所以這里把多余的變量留著就好了。2zS28資訊網——每日最新資訊28at.com

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# -> refactordef 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。所以這可以應用于字符串、元組、列表、字典和集合。2zS28資訊網——每日最新資訊28at.com

if len(list_of_hats) > 0:    hat_to_wear = choose_hat(list_of_hats)# -> refactorif list_of_hats:    hat_to_wear = choose_hat(list_of_hats)

本文鏈接:http://www.www897cc.com/showinfo-26-11875-0.html八個重構技巧使得Python代碼更Pythonic

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 聊一聊Java 21,虛擬線程、結構化并發和作用域值

下一篇: C++循環優化:提升性能的關鍵技巧

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 绥棱县| 观塘区| 宁安市| 金塔县| 赤水市| 昔阳县| 右玉县| 宣威市| 汪清县| 三明市| 天门市| 随州市| 房产| 鄂托克前旗| 越西县| 宜良县| 昭苏县| 南乐县| 昌江| 文化| 绵竹市| 昆山市| 方山县| 丰台区| 故城县| 玉屏| 疏附县| 南安市| 三门县| 临洮县| 梁河县| 德保县| 县级市| 上思县| 永春县| 定边县| 盐源县| 邢台县| 衡阳市| 丰顺县| 新余市|