跟我讀python3文檔:018_關(guān)于自定義函數(shù)的更多信息(4.7. More on Defining Functions)下

貫穿始終的理念:別廢話,就是干!


往期回顧

  • 自定義函數(shù)
  • 默認(rèn)參數(shù)
  • 關(guān)鍵字參數(shù)

文檔解讀

python3文檔第四小節(jié)鏈接地址:4.7. More on Defining Functions

本章主要講解4.7.2. Keyword Arguments(關(guān)鍵字參數(shù))4.7.3. Arbitrary Argument Lists(任意個(gè)數(shù)參數(shù))4.7.5. Lambda Expressions(Lambda表達(dá)式)這個(gè)小節(jié)


4.7.2. Keyword Arguments

段落截?。ㄒ唬?/h2>

When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except for those corresponding to a formal parameter.

名詞解釋

  1. dictionary
    字典類型
    形式像是下面這樣:
"name" : "galipy"   #單雙引號(hào)都可以

冒號(hào)左邊叫做“鍵”,其實(shí)就是名字的意思。冒號(hào)右邊叫做“值”。
一般會(huì)用大括號(hào)括起來(lái),并且有多個(gè)值,值與值之間用逗號(hào)分隔。

{'fruit' : 'apple', 'water' : 'coca-cola'}

這種用大括號(hào)括起來(lái),鍵值成對(duì)兒出現(xiàn)的類型就是字典類型。

段落大意

如果定義函數(shù)時(shí),最后有一個(gè)**name形式的參數(shù),他會(huì)接收多余的關(guān)鍵字參數(shù),并用字典類型表示出來(lái)。

我們直接看代碼事例:

>>> def func(country, name, **food):
...     print(country, name, food)
... 
>>> func("China", "galipy", fruit = "apple", water = "coca-cola")
China galipy {'fruit': 'apple', 'water': 'coca-cola'}
>>> 

看明白了嗎?我們只定義了三個(gè)參數(shù),但調(diào)用函數(shù)時(shí),傳遞了四個(gè)參數(shù),后兩個(gè)參數(shù)被轉(zhuǎn)為了字典類型。


4.7.3. Arbitrary Argument Lists

段落截取(二)

the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple . Before the variable number of arguments, zero or more normal arguments may occur. the form *args 。

名詞解釋

  1. arbitrary number of arguments
    任意數(shù)量參數(shù)
  2. tuple
    元組類型
    他和列表類型很像。但元組是不可改變的,用圓括號(hào)包裹。形式如下:
(1,3,5,4,34, 'galipy')

段落大意

可以用*args的形式指定多余參數(shù)的存儲(chǔ)位置。在調(diào)用函數(shù)時(shí),多余的非關(guān)鍵字參數(shù),將被以元組的形式,存入args中??勺償?shù)量參數(shù)前,是可以有正常參數(shù)的。

我們直接來(lái)看代碼:

>>> def num_print( number1, *args_tuple ):  
···    print ("輸出: ")
···    print (number1)
···    print(args_tuple)
···
>>> num_print(456)
456
()
>>> num_print(1, 2, 3, 5, 6)
1
(2, 3, 5, 6)

看明白了嗎?能理解為什么我們第一次調(diào)用num_print函數(shù)時(shí),會(huì)單獨(dú)輸出一對(duì)兒括號(hào)嗎?


4.7.5. Lambda Expressions

段落截?。ㄈ?/h2>

Small anonymous functions can be created with the lambda keyword.

名詞解釋

  1. anonymous functions
    匿名函數(shù)
    就是字面意思,沒有名字的函數(shù)。

  2. lambda
    它是定義匿名函數(shù)的關(guān)鍵字,相當(dāng)于def關(guān)鍵字

段落大意

我們可以用lambda關(guān)鍵字創(chuàng)建匿名函數(shù)。

我們直接看代碼解釋:

>>> sum = lambda a, b: a + b;
>>> sum(1, 2)
3

注意lambda的語(yǔ)法結(jié)構(gòu)

  • lambda關(guān)鍵字
  • 參數(shù)
  • 分號(hào)
  • 運(yùn)算表達(dá)式

這是求兩數(shù)和的函數(shù),比較難理解的地方是調(diào)用匿名函數(shù)這一步。
看看它是如何調(diào)用的,并思考下面的問(wèn)題

思考:看看下面這個(gè)包含匿名函數(shù)的函數(shù)應(yīng)該如何調(diào)用

>>> def make_incrementor(n):
...     return lambda x: x + n
...

提示:這依舊是計(jì)算兩數(shù)和的函數(shù),參考我們上一個(gè)例子,試試看~


自定義函數(shù)參數(shù)注意事項(xiàng)總結(jié)

  • 位置參數(shù)
  • 默認(rèn)參數(shù)
  • 關(guān)鍵字參數(shù)
  • 不定長(zhǎng)位置參數(shù)
  • 不定長(zhǎng)關(guān)鍵字參數(shù)
    以上就是我們?cè)谡{(diào)用函數(shù)時(shí),會(huì)輸入的參數(shù)形式。
    看看是不是都能明白這些參數(shù)的形式,以及他們調(diào)用順序和存儲(chǔ)形式。
    比如,位置參數(shù)的順序;比如誰(shuí)會(huì)以元組類型存儲(chǔ),誰(shuí)會(huì)以字典類型存儲(chǔ)等。

第四節(jié)就講解這么多,這里省略了:
4.7.4. Unpacking Argument Lists
4.7.6. Documentation Strings
4.7.7. Function Annotations
4.8. Intermezzo: Coding Style
四個(gè)小節(jié),感興趣或者想自我提高的同學(xué)可自行閱讀。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容