1)可變位置參數
在Python中,函數在定義時可以擁有任意數量的參數,這種參數稱為可變參數。可以通過定義可變參數,來接收調用函數時多余的參數。可變參數又分為可變位置參數和可變關鍵字參數,它們的區別如下:
①可變位置參數用來接收調用函數時多余的位置參數;在函數體內,可變位置參數是一個元組。
②可變關鍵字參數用來接收調用函數時多余的關鍵字參數;在函數體內,可變關鍵字參數是一個字典。
可變位置參數是在普通的參數前面加一個星號“*”,一般命名為args(arguments的縮寫),但實際上它可以用任意合法的名稱:
- >>> def f(*args): # *args是可變位置參數
- ... print(args) # 打印args
- ... print(type(args)) # 打印args的類型
- ... for i in args: # 迭代元組
- ... print(i)
- ...
- >>> f('Python', 42, 3.14)
- ('Python', 42, 3.14)
- <class 'tuple'>
- Python
- 42
- 3.14
由運行結果可知,可變位置參數在函數體內是一個元組。另外,函數體內的args不需要加星號。
在定義函數時,如果不確定所需要的參數個數,那么可以使用可變參數。假設要寫一個算術加法運算的程序,不使用可變參數時,只能將確定個數的數字相加:
- >>> def add_numbers(a, b, c): # 這個函數只能讓三個數字相加
- ... print(a + b + c)
- ...
- >>> add_numbers(1, 2, 3)
- 6
如果使用可變參數,那么可以實現讓任意個數的數字相加:
- >>> def add_numbers(*numbers): # 將可變位置參數命名為numbers
- ... sum = 0
- ... for i in numbers: # 由于numbers是元組,因此,可以使用for循環迭代
- ... sum += i
- ... print(sum)
- ...
- >>> add_numbers(1, 2, 3, 4, 5, 6)
- 21
- >>> add_numbers(42, 19, 25)
- 86
- >>> add_numbers() # 可變位置參數也可以傳遞0個參數
- 0
可變位置參數可以與普通的參數混用。假設要打印一份水果店的公告,其中第一個參數是普通的參數,代表水果店的名字,第二個參數是可變位置參數,用來接收除了水果店名字之外的其他位置參數:
- >>> def fruit_shop(shop_name, *fruits):
- ... print('{0}水果店開業啦!'.format(shop_name))
- ... print('在售的水果有:')
- ... for fruit in fruits:
- ... print(fruit)
- ...
- >>> fruit_shop('小明', '蘋果', '香蕉', '西瓜')
- 小明水果店開業啦!
- 在售的水果有:
- 蘋果
- 香蕉
- 西瓜
函數調用時,'小明'被shop_name接收,剩余的值都被*fruits接收,并存儲在fruits元組中。
2)可變關鍵字參數
可變關鍵字參數是在普通的參數前面加兩個星號“**”,一般命名為kwargs(keyword arguments的縮寫),但實際上它可以用任意合法的名稱:
- >>> def f(**kwargs): # **kwargs是可變關鍵字參數
- ... print(kwargs)
- ... print(type(kwargs)) # 打印kwargs的類型
- ... for k, w in kwargs.items(): # 迭代字典
- ... print('{0}--{1}'.format(k, w))
- ...
- >>> f(name='Ming', age=19) # 使用關鍵字參數才能將值存儲到kwargs中
- {'name': 'Ming', 'age': 19}
- <class 'dict'>
- name--Ming
- age--19
- >>> f() # 可變關鍵字參數也可以傳遞0個參數
- {}
可變關鍵字參數可以與普通的參數混用。假設在前面的“水果店”程序中,不僅打印水果的名稱,還打印水果的個數:
- >>> def fruit_shop_v2(shop_name, **fruits):
- ... print('{0}水果店開業啦!'.format(shop_name))
- ... print('在售的水果有:')
- ... for fruit, count in fruits.items(): # 迭代字典
- ... print('{0}{1}個'.format(fruit, count))
- ...
- >>> fruit_shop_v2(shop_name='小明', 蘋果=10, 香蕉=3, 橘子=201)
- 小明水果店開業啦!
- 在售的水果有:
- 蘋果10個
- 香蕉3個
- 橘子201個
下面是一個更復雜的“水果店”程序,四個參數中,shop_name代表商店名稱,open_time代表開業時間,*fruits代表水果種類,**other_info代表水果店還想打印的額外信息:
- >>> def fruit_shop_v3(shop_name, open_time='10月1日', *fruits, **other_info):
- ... print('{0}水果店將在{1}開業!'.format(shop_name, open_time))
- ... if fruits:
- ... print('在售的水果有:')
- ... for fruit in fruits:
- ... print(fruit)
- ... if other_info:
- ... print('以下是額外信息:')
- ... for item, info in other_info.items():
- ... print('{0}:{1}'.format(item, info))
- ...
- >>> fruit_shop_v3('小明') # shop_name既沒默認值,又不是可變參數,故不能缺省
- 小明水果店將在10月1日開業!
- >>> fruit_shop_v3('小明', '1月1日') # 提供shop_name和open_time
- 小明水果店將在1月1日開業!
- >>> fruit_shop_v3(open_time='1月1日') # 如果不指定shop_name的值,會報錯
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: fruit_shop_v3() missing 1 required positional argument: 'shop_name'
- >>> fruit_shop_v3('小明', '蘋果', '香蕉', '橘子')
- 小明水果店將在蘋果開業!
- 在售的水果有:
- 香蕉
- 橘子
- >>> fruit_shop_v3('小明', '10月1日', '蘋果', '香蕉', '橘子')
- 小明水果店將在10月1日開業!
- 在售的水果有:
- 蘋果
- 香蕉
- 橘子
- >>>
- >>> fruit_shop_v3('小明', '10月1日', '蘋果', '香蕉', 地址='北京市', 開店折扣='八折') # 多余參數全部被**other_info接收
- 小明水果店將在10月1日開業!
- 在售的水果有:
- 蘋果
- 香蕉
- 以下是額外信息:
- 地址:北京市
- 開店折扣:八折
>>本文地址:http://www.jecan.cn/zhuanye/2021/66399.html
聲明:本站稿件版權均屬中公教育優就業所有,未經許可不得擅自轉載。
1 您的年齡
2 您的學歷
3 您更想做哪個方向的工作?