<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Python `*args`和`**kwargs` > 原文: [https://thepythonguru.com/python-args-and-kwargs/](https://thepythonguru.com/python-args-and-kwargs/) * * * 于 2020 年 1 月 7 日更新 * * * 什么是`*args`? `*args`允許我們將可變數量的參數傳遞給函數。 讓我們以一個例子來闡明這一點。 假設您創建了一個將兩個數字相加的函數。 ```py def sum(a, b): ? ? print("sum is", a+b) ``` 如您所見,該程序僅接受兩個數字,如果您要傳遞兩個以上的參數,這就是`*args`起作用的地方。 ```py def sum(*args): ? ? s = 0 ? ? for i in args: ? ? ? ? s += i ? ? print("sum is", s) ``` 現在,您可以像這樣將任意數量的參數傳遞給函數, ```py >>> sum(1, 2, 3) 6 >>> sum(1, 2, 3, 4, 5, 7) 22 >>> sum(1, 2, 3, 4, 5, 7, 8, 9, 10) 49 >>> sum() 0 ``` **注意**: `*args`的名稱只是一個約定,您可以使用任何有效標識符。 例如`*myargs`是完全有效的。 ## 什么是`**kwargs`? * * * `**kwargs`允許我們傳遞可變數量的關鍵字參數,例如`func_name(name='tim', team='school')` ```py def my_func(**kwargs): ? ? for i, j in kwargs.items(): ? ? ? ? print(i, j) my_func(name='tim', sport='football', roll=19) ``` **預期輸出**: ```py sport football roll 19 name tim ``` ## 在函數調用中使用`*args`和`**kwargs` * * * 您可以使用`*args`將可迭代變量中的元素傳遞給函數。 以下示例將清除所有內容。 ```py def my_three(a, b, c): ? ? print(a, b, c) a = [1,2,3] my_three(*a) # here list is broken into three elements ``` **注意**: 僅當參數數量與可迭代變量中的元素數量相同時,此方法才有效。 同樣,您可以使用`**kwargs`來調用如下函數: ```py def my_three(a, b, c): ? ? print(a, b, c) a = {'a': "one", 'b': "two", 'c': "three" } my_three(**a) ``` 請注意,要使此工作有效,需要做兩件事: 1. 函數中的參數名稱必須與字典中的鍵名稱匹配。 2. 參數的數量應與字典中的鍵的數量相同。 * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看