1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        簡化Python函數(shù)調(diào)用的3種技巧

        共 2141字,需瀏覽 5分鐘

         ·

        2021-02-06 19:17


        大家好,歡迎來到 Crossin的編程教室 !

        今天給大家分享一個(gè)關(guān)于編程技巧的討論。

        假設(shè)有一個(gè)函數(shù),這個(gè)函數(shù)需要接收4個(gè)參數(shù),并返回這4個(gè)參數(shù)的和:

        def?sum_four(a, b, c, d):
        ??return?a + b + c + d


        如果需要固定最后前三個(gè)參數(shù),僅改變最后一個(gè)參數(shù)的值,這時(shí)候可能需要這么調(diào)用:

        >>> a, b, c = 1, 2, 3

        >>> sum_four(a=a, b=b, c=c, d=1)
        7

        >>> sum_four(a=a, b=b, c=c, d=2)
        8

        >>> sum_four(a=a, b=b, c=c, d=3)
        9

        >>> sum_four(a=a, b=b, c=c, d=4)
        10


        這樣寫實(shí)在是太丑了,如果用 Map 函數(shù),是否能簡化代碼?

        答案是肯定的,但是Map函數(shù)“一般”只能接受單一元素,如果你強(qiáng)行使用的話,它會(huì)報(bào)這樣的錯(cuò):

        >>> list(map(sum_four, [(1, 2, 3, 4)]))
        Traceback (most recent call?last):
        ??File "", line?1, in
        TypeError: sum_four() missing 3?required positional arguments:?'b', 'c', and?'d'


        怎么解決?

        方案1: itertools.starmap

        可以使用 itertools 的函數(shù) starmap 替換Map.

        它與Map不同,允許接受一個(gè)元組作為傳入sum_four的參數(shù)。

        >>> import?itertools
        >>> list(itertools.starmap(sum_four, [(1, 2, 3, 4)]))
        [10]


        非常棒,這樣的話,上述問題就可以使用starmap函數(shù)解決:

        >>> import?itertools

        >>> ds = [1, 2, 3, 4]

        >>> items = ((a, b, c, d) for?d in?ds)

        >>> list(items)
        ?[(1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 2, 3, 4)]

        >>> list(itertools.starmap(sum_four, items))
        ?[7, 8, 9, 10]


        請注意 items 是一個(gè)生成器,這是為了避免 items 過大導(dǎo)致內(nèi)存消耗量過大。平時(shí)開發(fā)的時(shí)候注意這些細(xì)節(jié),能夠使你和普通的開發(fā)者拉開差距。

        方案2: functools.partial

        第二種解決方案是使用 partial 函數(shù)固定前三個(gè)參數(shù)。

        根據(jù)文檔,partial將“凍結(jié)”函數(shù)的參數(shù)的某些部分,從而生成簡化版的函數(shù)。

        因此上述問題的解決方案就是:

        >>> import?functools
        >>> partial_sum_four = functools.partial(sum_four, a, b, c)
        >>> partial_sum_four(3)
        9
        >>> # 這樣就可以使用map函數(shù)了:
        >>> list(map(partial_sum_four, ds))
        [7, 8, 9, 10]

        方案3: itertools.repeat()

        事實(shí)上,Map 函數(shù)是允許傳遞可迭代參數(shù)的,但是有一個(gè)有趣的特點(diǎn),他會(huì)用每個(gè)可迭代對象里的項(xiàng)作為傳入函數(shù)的不同參數(shù)。這樣說可能太過于抽象了,來看看實(shí)際的例子:

        >>> list(map(sum_four, [1,1,1,1], [2,2,2,2], [3,3,3,3], [1,2,3,4]))
        ?[7, 8, 9, 10]


        明顯,每次都使用了不同數(shù)組中對應(yīng)下標(biāo)的項(xiàng)傳入函數(shù)進(jìn)行計(jì)算。

        這樣,我們可以使用這個(gè)特點(diǎn)進(jìn)行優(yōu)化。

        itertools.repeat() 函數(shù)能夠根據(jù)參數(shù)產(chǎn)生一個(gè)迭代器,該迭代器一次又一次返回對象。不指定times參數(shù),它將無限期運(yùn)行。

        而 Map 函數(shù)會(huì)在最短的可迭代對象被迭代完后,就會(huì)自動(dòng)停止運(yùn)行。

        結(jié)合這兩個(gè)特點(diǎn),上述問題的解決方案就出來了:

        >>> import?itertools
        >>> list(map(sum_four, itertools.repeat(a), itertools.repeat(b), itertools.repeat(c), ds))
        ?[7, 8, 9, 10]


        這招非常巧妙,缺點(diǎn)是能讀懂的人不多。不過沒關(guān)系,計(jì)算機(jī)世界中某些東西知道就好,你并不一定需要去使用它。


        比如本文中的這幾種解決方案,日常生活工作中一般用不到,所以你不需要死記硬背,但你需要知道“有這樣的問題”和“有這些解決方案”,萬一遇到了相似的場景,你就可以回憶起這篇文章并快速找到解決的方法。


        今天分享的文章就到此結(jié)束啦,如果對你有幫助,歡迎轉(zhuǎn)發(fā)/點(diǎn)贊/收藏!

        作者:Ckend
        來源:Python實(shí)用寶典


        _往期文章推薦_

        寫好Python函數(shù)的幾點(diǎn)建議




        瀏覽 36
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            性做久久久久久 | 男男被狂c躁到高潮失禁网站 | 91人妻人人操人人爽人人精品 | 五月色婷婷色婷婷 | 69操逼网站 | 美女扒开粉嫩的尿囗 | 国产56页 | 北条麻妃黄色视频免费播放 | 国产无码精品在线观看 | 囯产精品久久欠久久久久久 |