激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 詳解Python 函數參數的拆解

詳解Python 函數參數的拆解

2020-09-02 23:57Yanbin Python

這篇文章主要介紹了Python 函數參數的拆解,幫助大家更好的理解和學習python,感興趣的朋友可以了解下

本文為閱讀 《Python Tricks: The Book》一書的 3.5 Function Argument Unpacking 的筆記與擴充理解。函數參數拆解是定義可變參數(VarArgs) *args**kwargs 的反向特性。

*args**kwars 是函數可定義一個形參來接收傳入的不定數量的實參。

而這里的函數參數拆解是形參定義多個,在調用時只傳入一個集合類型對象(帶上 * 或 ** 前綴),如 list, tuple, dict, 甚至是 generator, 然后函數能自動從集合對象中取得對應的值。

如果能理解下面賦值時的參數拆解和 Python 3.5 的新增 * ** 操作,那么于本文講述的特性就好理解了。

唯一的不同時作為參數的集合傳入函數時必須前面加上 ***, 以此宣告該參數將被拆解,而非一個整體作為一個函數參數。加上 * ** 與 Java 的 @SafeVarargs 有類似的功效,最接近的是 Scala 的 foo(Array[String]("d", "e") : _*) 寫法。參見:Java 和 Scala 調用變參的方式

Python 的賦值拆解操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> a, b = [1, 2# a, b = (1, 2) 也是一樣的效果
>>> print(a, b)
1 2
>>> a, b = {'x': 1, 'y':2}
>>> print(a, b)
x y
>>> a, b = {'x': 1, 'y':2}.keys()
>>> print(a, b)
x y
>>> a, b = {'x': 1, 'y':2}.values()
>>> print(a, b)
1 2
>>> a, b = (x * x for x in range(2))
>>> print(a, b)
0 1

Python 3.5 的新增拆解操作

?
1
2
3
4
>>> [1, 2, *range(3), *[4, 5], *(6, 7)]  # * 號能把集合打散,flatten(unwrap)
[1, 2, 0, 1, 2, 4, 5, 6, 7]
>>> {'x': 1, **{'y': 2, 'z': 3}}      # ** 把字典打散, flatten(unwrap) 操作
{'x': 1, 'y': 2, 'z': 3}

有些像是函數編程中的 flatten unwrap 操作。

有了上面的基礎后,再回到原書中的例子,當我們定義如下打印 3-D 坐標的函數

?
1
2
def print_vector(x, y, z):
  print('<%s, %s, %s>' % (x, y, z))

依次傳入三個參數的方式就不值不提了,現在就看如何利用函數的參數拆解特性,只傳入一個集合參數,讓該 print_vector 函數準確從集合中獲得相應的 x, y, 和 z 的值。

函數參數拆解的調用舉例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> list_vec = [2, 1, 3]
>>> print_vector(*list_vec)
<2, 1, 3>
>>> print_vector(*(2, 1, 3))
<2, 1, 3>
>>> dict_vec = {'y': 2, 'z': 1, 'x': 3}
>>> print_vector(*dict_vec)  # 相當于 print_vector(*dict_vec.keys())
<y, z, x>
>>> print_vector(**dict_vec)  # 相當于 print_vector(dict_vec['x'], dict_vec['y'], dict_vec['z']
<3, 2, 1>
>>> genexpr = (x * x for x in range(3))
>>> print_vector(*genexpr)
<0, 1, 4>
>>> print_vector(*dict_vec.values()) # 即 print_vector(*list(dict_vec.values()))
<2, 1, 3>

注意 **dict_vec 有點不一樣,它的內容必須是函數 print_vector 的形參 'x', 'y', 'z' 作為 key 的三個元素。

以下是各種錯誤

**dict_vec 元素個數不對,或 key 不匹配時的錯誤

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> print_vector(**{'y': 2, 'z': 1, 'x': 3})
<3, 2, 1>
>>> print_vector(**{'y': 2, 'z': 1, 'a': 3})    #元素個數是3 個,但出現 x, y, z 之外的 key
Traceback (most recent call last):
 File "<pyshell#39>", line 1, in <module>
  print_vector(**{'y': 2, 'z': 1, 'a': 3})
TypeError: print_vector() got an unexpected keyword argument 'a'
>>> print_vector(**{'y': 2, 'z': 1, 'x': 3, 'a': 4}) # 包含有 x, y, z, 但有四個元素,key 'a' 不能識別
Traceback (most recent call last):
 File "<pyshell#40>", line 1, in <module>
  print_vector(**{'y': 2, 'z': 1, 'x': 3, 'a': 4})
TypeError: print_vector() got an unexpected keyword argument 'a'
>>> print_vector(**{'y': 2, 'z': 1})     # 缺少 key 'x' 對應的元素
Traceback (most recent call last):
 File "<pyshell#41>", line 1, in <module>
  print_vector(**{'y': 2, 'z': 1})
TypeError: print_vector() missing 1 required positional argument: 'x'

不帶星星的錯誤

?
1
2
3
4
5
>>> print_vector([2, 1, 3])
Traceback (most recent call last):
 File "<pyshell#44>", line 1, in <module>
  print_vector([2, 1, 3])
TypeError: print_vector() missing 2 required positional arguments: 'y' and 'z'

把集合對象整體作為第一個參數,所以未傳入 y 和 z,因此必須用前綴 * 或 ** 通告函數進行參數拆解

集合長度與函數參數個數不匹配時的錯誤

?
1
2
3
4
5
6
7
8
9
10
>>> print_vector(*[2, 1])        # 拆成了 x=2, y=1, 然后 z 呢?
Traceback (most recent call last):
 File "<pyshell#47>", line 1, in <module>
  print_vector(*[2, 1])
TypeError: print_vector() missing 1 required positional argument: 'z'
>>> print_vector(*[2, 1, 3, 4])    # 雖然拆出了 x=2, y=1, z=3, 但也別想強塞第四個元素給該函數(只定義的三個參數)
Traceback (most recent call last):
 File "<pyshell#48>", line 1, in <module>
  print_vector(*[2, 1, 3, 4])
TypeError: print_vector() takes 3 positional arguments but 4 were given

上面這兩個錯誤與賦值時的拆解因元素個數不匹配時的錯誤是相對應的

?
1
2
3
4
5
6
7
8
9
10
>>> a, b = [1]
Traceback (most recent call last):
 File "<pyshell#54>", line 1, in <module>
  a, b = [1]
ValueError: not enough values to unpack (expected 2, got 1)
>>> a, b = [1, 2, 3]
Traceback (most recent call last):
 File "<pyshell#55>", line 1, in <module>
  a, b = [1, 2, 3]
ValueError: too many values to unpack (expected 2)

當然在賦值時 Python 可以像下面那樣做

?
1
2
3
a, b, *c = [1, 2, 3, 4]
>>> print(a, b, c)
1 2 [3, 4]

補充(2020-07-02): 迭代的拆解在 Python 中的術語是 Iterable Unpacking, 找到兩個相關的 PEP 448, PEP 3132。在實際上用處還是很大的,比如在拆分字符串時只關系自己有興趣的字段

?
1
2
3
4
5
6
line = '2020-06-19 22:14:00    2688 abc.json'
date, time, size, name = line.split()  # 獲得所有字段值
_, time, _, name = line.split()     # 只對 time 和 name 有興趣
date, *_ = line.split()         # 只對第一個 date 有興趣
*_, name = line.split()         # 只對最后的 name 有興趣
date, *_, name = line.split()      # 對兩邊的 date, name 有興趣

這樣就避免了用索引號來引用拆分后的值,如 split[0], splint[2] 等,有名的變量不容易出錯。注意到 Python 在拆解時非常聰明,它知道怎么去對應位置,用了星號(*) 的情況,明白如何處理前面跳過多少個,中間跳過多少個,或最后收集多少個元素。

鏈接:

PEP 448 -- Additional Unpacking Generalizations
PEP 3132 -- Extended Iterable Unpacking

以上就是詳解Python 函數參數的拆解的詳細內容,更多關于python 函數參數拆解的資料請關注服務器之家其它相關文章!

原文鏈接:https://yanbin.blog/python-function-argument-unpacking/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 哪里可以看免费的av | 成人免费观看49www在线观看 | 一级α片免费看刺激高潮视频 | 国外成人在线视频 | 欧美综合在线观看 | 俄罗斯16一20sex牲色另类 | 亚洲国产精品99 | 在线成人免费网站 | 亚洲射情| 最新中文字幕第一页视频 | 日韩黄色一级视频 | 亚洲成人精品国产 | 欧美成人精品一区二区 | 一区二区三区欧美在线观看 | 久草在线免费资源站 | 一级一级一级一级毛片 | a一级黄色大片 | 羞羞的小视频 | 免费观看9x视频网站在线观看 | 得得啪在线| 悠悠成人资源亚洲一区二区 | 国产欧美日韩视频在线观看 | 在线成人毛片 | 一区二区国产在线 | 国产亚洲精品久久久久5区 综合激情网 | 特级西西444www大精品视频免费看 | 精品一区二区三区免费毛片 | 日韩2区 | 线观看免费完整aaa 久久不雅视频 | 亚洲免费观看视频 | 麻豆一区二区99久久久久 | 国产精品www | 一级黄色毛片播放 | 黄色片快播 | 日本a∨精品中文字幕在线 欧美1—12sexvideos | 久久久久99精品 | 在线播放免费人成毛片乱码 | 精品一区二区免费 | 欧美成人综合视频 | 欧美日韩在线视频一区 | 久久不射电影网 |