<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何使用裝飾器簡化大量 if…elif…代碼?

          共 2779字,需瀏覽 6分鐘

           ·

          2021-11-28 13:11

          ↑?關(guān)注 + 星標(biāo)?,每天學(xué)Python新技能

          后臺(tái)回復(fù)【大禮包】送你Python自學(xué)大禮包

          今天在 Github 閱讀EdgeDB[1]的代碼,發(fā)現(xiàn)它在處理大量if...elif...else判斷的時(shí)候,使用了一個(gè)非常巧妙的裝飾器。我們來看看這個(gè)方法具體是什么樣的。

          正好今天是雙十一,假設(shè)我們要做一個(gè)功能,根據(jù)用戶的等級(jí)判斷他可以獲得的折扣。常規(guī)的if ... elif...寫法是這樣的:

          def?get_discount(level):
          ????if?level?==?1:
          ????????"大量計(jì)算代碼"
          ????????discount?=?0.1
          ????elif?level?==?2:
          ????????"大量計(jì)算代碼"
          ????????discount?=?0.2
          ????elif?level?==?3:
          ????????discount?=?0.3
          ????elif?level?==?4:
          ????????discount?=?0.4
          ????elif?level?==?5:
          ????????discount?=?0.5
          ????elif?level?==?6:
          ????????discount?=?3?+?2?-?5?*?0.1
          ????else:
          ?????????return?'等級(jí)錯(cuò)誤'
          ????return?discount

          大家都知道,這樣大量的if ... elif...代碼非常難看,也很難維護(hù)。并且每個(gè) if 的內(nèi)部有很多代碼。這個(gè)函數(shù)就會(huì)被拉得非常長。

          有一些同學(xué)知道,可以使用字典來改寫這個(gè)太長的 if 判斷:

          def?parse_level_1():
          ????"大量計(jì)算代碼"
          ????discount?=?0.1
          ????return?discount

          def?parse_level_2():
          ????"大量計(jì)算代碼"
          ????discount?=?0.2
          ????return?discount

          def?parse_level_3():
          ????"大量計(jì)算代碼"
          ????discount?=?0.3
          ????return?discount

          def?parse_level_4():
          ????"大量計(jì)算代碼"
          ????discount?=?0.4
          ????return?discount

          def?parse_level_5():
          ????"大量計(jì)算代碼"
          ????discount?=?0.5
          ????return?discount

          def?parse_level_6():
          ????"大量計(jì)算代碼"
          ????discount?=?3?+?2?-?5?*?0.1
          ????return?discount

          discount_map?=?{
          ?1:?parse_level_1,
          ??2:?parse_level_2,
          ??3:?parse_level_3,
          ??4:?parse_level_4,
          ??5:?parse_level_5,
          ??6:?parse_level_6,
          }

          discount?=?discount_map.get(level,?'等級(jí)錯(cuò)誤')

          但今天我學(xué)到的這個(gè)方法,比用字典更簡單。我們先來看它的效果:

          @value_dispatch
          def?get_discount(level):
          ????return?'等級(jí)錯(cuò)誤'

          @get_discount.register(1)
          def?parse_level_1(level):
          ????"大量計(jì)算代碼"
          ????discount?=?0.1
          ????return?discount

          @get_discount.register(2)
          def?parse_level_2(level):
          ????"大量計(jì)算代碼"
          ????discount?=?0.2
          ????return?discount

          @get_discount.register(3)
          def?parse_level_3(level):
          ????"大量計(jì)算代碼"
          ????discount?=?0.3
          ????return?discount

          @get_discount.register(4)
          def?parse_level_4(level):
          ????"大量計(jì)算代碼"
          ????discount?=?0.4
          ????return?discount

          @get_discount.register(5)
          def?parse_level_5(level):
          ????"大量計(jì)算代碼"
          ????discount?=?0.5
          ????return?discount

          @get_discount.register(6)
          def?parse_level_1(level):
          ????"大量計(jì)算代碼"
          ????discount?=?3?+?2?-?5?*?0.1
          ????return?discount


          discount?=?get_discount(3)
          print(f'等級(jí)3的用戶,獲得的折扣是:{discount}')

          運(yùn)行效果如下圖所示:

          這樣寫,比用字典的方式更直觀,比直接用if ... elif...更簡潔。

          那么,這個(gè)裝飾器value_dispatch是怎么實(shí)現(xiàn)的呢?密碼就藏在這個(gè)開源項(xiàng)目EdgeDB源代碼[2]中,核心代碼只有20多行:

          并且,還能夠?qū)崿F(xiàn)或查詢。例如用戶等級(jí)為2或者3的時(shí)候,折扣都是0.2,那么代碼可以寫成:

          @get_discount.register(2)
          @get_discount.register(3)
          def?parse_level_2(level):
          ????"大量計(jì)算代碼"
          ????discount?=?0.2
          ????return?discount

          運(yùn)行效果如下圖所示:

          它這個(gè)代碼目前只能實(shí)現(xiàn)相等的查詢。但其實(shí)只要對這個(gè)代碼稍作修改,我們就能實(shí)現(xiàn)大于、小于、大于等于、小于等于、不等于、in等等判斷。如果大家有興趣的話,請?jiān)谖恼孪旅媪粞裕覀兠魈炀蛠碚f說怎么對這個(gè)代碼進(jìn)行改造,實(shí)現(xiàn)更多的邏輯判斷。

          參考文獻(xiàn)

          [1]?EdgeDB:?https://github.com/edgedb/edgedb

          [2] 源代碼:?https://github.com/edgedb/edgedb/blob/master/edb/common/value_dispatch.py


          推薦閱讀

          1. Excel 已死,爬蟲已涼,數(shù)據(jù)分析稱王!

          2. 丟掉Pycharm,這款神器值得擁有!

          3. 零代碼爬蟲神器 -- Web Scraper 的使用

          4. 用Python玩金融是什么樣的體驗(yàn)?

          5. 比 Python 快得嚇人,PyPy 極簡入門


          瀏覽 72
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  美国黑人一级大黄 | 午夜福利一区二区三区 | 国产精品无套久久久久 | 成人电影一二三区 | 日本黄色视频官网 |