久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Python 帶星號(hào)(* 或 **)的函數(shù)參數(shù)詳解

瀏覽:3日期:2022-06-27 11:07:15
1. 帶默認(rèn)值的參數(shù)

在了解帶星號(hào)(*)的參數(shù)之前,先看下帶有默認(rèn)值的參數(shù),函數(shù)定義如下:

>> def defaultValueArgs(common, defaultStr = 'default', defaultNum = 0): print('Common args', common) print('Default String', defaultStr) print('Default Number', defaultNum)

(1)帶默認(rèn)值的參數(shù)(defaultStr、defaultNum)不傳參時(shí)的調(diào)用:

>> defaultValueArgs('Test') Common args TestDefault String defaultDefault Number 0

(2)帶默認(rèn)值的參數(shù)(defaultStr、defaultNum),調(diào)用的時(shí)候可以直接傳參(如下例中的defaultStr),也可以寫成“argsName = value”的形式(如下例中的defaultNum):

>> defaultValueArgs('Test', 'Str', defaultNum = 1) Common args TestDefault String StrDefault Number 1 >> defaultValueArgs('Test', defaultNum = 1) Common args TestDefault String defaultDefault Number 1

注意:在函數(shù)定義時(shí),第一個(gè)帶有默認(rèn)值的參數(shù)之后的所有參數(shù)都必須有默認(rèn)值,否則,運(yùn)行時(shí)報(bào)錯(cuò)。

>> def defaultValueArgs(common, defaultStr = 'default', defaultNum): print('Common args', common) print('Default String', defaultStr) print('Default Number', defaultNum) SyntaxError: non-default argument follows default argument 2.帶一個(gè)星號(hào)(*)的函數(shù)參數(shù)

帶一個(gè)參數(shù)的函數(shù)定義如下:

>> def singalStar(common, *rest): print('Common args: ', common) print('Rest args: ', rest)

(1)帶星號(hào)(*)的參數(shù)不傳參:

>> singalStar('hello') Common args: helloRest args: ()

帶星號(hào)(*)的參數(shù)不傳參時(shí)默認(rèn)是一個(gè)空的元組。

(2)帶星號(hào)(*)的參數(shù)傳入多個(gè)值時(shí)(個(gè)數(shù)大于或等于函數(shù)定義時(shí)的參數(shù)個(gè)數(shù)):

>> singalStar('hello', 'world', 000) Common args: helloRest args: (’world’, 0)

不難看出,第二種方式中,星號(hào)參數(shù)把接收的多個(gè)參數(shù)合并為一個(gè)元組。

(3)當(dāng)我們直接傳元組類型的值給星號(hào)參數(shù)時(shí):

>> singalStar('hello', ('world', 000)) Common args: helloRest args: ((’world’, 0),)

此時(shí),傳遞的元組值作為了星號(hào)參數(shù)的元組中的一個(gè)元素。

(4)如果我們想把元組作為星號(hào)參數(shù)的參數(shù)值,在元組值前加上' * ' 即可。

>> singalStar('hello', *('world', 000))Common args: helloRest args: (’world’, 0)>> singalStar('hello', *('world', 000), '123')Common args: helloRest args: (’world’, 0, ’123’)3.帶兩個(gè)星號(hào)(**)的函數(shù)參數(shù)

帶兩個(gè)星號(hào)(**)的函數(shù)定義如下:

>> def doubleStar(common, **double): print('Common args: ', common) print('Double args: ', double)

(1)雙星號(hào)(**)參數(shù)不傳值:

>> doubleStar('hello') Common args: helloDouble args: {}

帶雙星號(hào)(**)的參數(shù)不傳值時(shí)默認(rèn)是一個(gè)空的字典。

(2)雙星號(hào)(**)參數(shù)傳入多個(gè)參數(shù)時(shí)(個(gè)數(shù)大于或等于函數(shù)定義時(shí)的參數(shù)個(gè)數(shù)):

>> doubleStar('hello', 'Test', 24)TypeError: doubleStar() takes 1 positional argument but 3 were given>> doubleStar('hello', x = 'Test', y = 24)Common args: helloDouble args: {’x’: ’Test’, ’y’: 24}

可以看到,雙星號(hào)參數(shù)把接收的多個(gè)參數(shù)合并為一個(gè)字典,但與單星號(hào)不同的是,此時(shí)必須采用默認(rèn)值傳參的 “ args = value ” 的方式,“ = ” 前的字段成了字典的鍵,“ = ” 后的字段成了字典的值。

(3)如果想把字典作為星號(hào)參數(shù)的參數(shù)值,那么該怎么辦呢?與單星號(hào)參數(shù)類似,在字典值前加上 “ ** ”,同時(shí)其后不能添加任何值。

>> doubleStar('hello', {'name': 'Test', 'age': 24})TypeError: doubleStar() takes 1 positional argument but 2 were given>> doubleStar('hello', **{'name': 'Test', 'age': 24}, {'name': 'Test2', 'age': 24})SyntaxError: positional argument follows keyword argument unpacking>> doubleStar('hello', **{'name': 'Test', 'age': 24}, **{'name': 'Test2', 'age': 24})TypeError: doubleStar() got multiple values for keyword argument ’name’>> doubleStar('hello', **{'name': 'Test', 'age': 24})Common args: helloDouble args: {’name’: ’Test’, ’age’: 24}4、在有些情況下,單星號(hào)函數(shù)參數(shù)和雙星號(hào)函數(shù)參數(shù)是一起使用的:

def singalAndDoubleStar(common, *single, **double): print('Common args: ', common) print('Single args: ', single) print('Double args: ', double)singalAndDoubleStar('hello')# Common args: hello# Single args: ()# Double args: {}singalAndDoubleStar('hello', 'world', 000)# Common args: hello# Single args: (’world’, 0)# Double args: {}singalAndDoubleStar('hello', 'world', 000, {'name': 'Test', 'age': 24})# Common args: hello# Single args: (’world’, 0, {’name’: ’Test’, ’age’: 24})# Double args: {}singalAndDoubleStar('hello', 'world', 000, **{'name': 'Test', 'age': 24})# Common args: hello# Single args: (’world’, 0)# Double args: {’name’: ’Test’, ’age’: 24}singalAndDoubleStar('hello', ('world', 000), {'name': 'Test', 'age': 24})# Common args: hello# Single args: ((’world’, 0), {’name’: ’Test’, ’age’: 24})# Double args: {}singalAndDoubleStar('hello', *('world', 000), {'name': 'Test', 'age': 24}) # Common args: hello# Single args: (’world’, 0, {’name’: ’Test’, ’age’: 24})# Double args: {}singalAndDoubleStar('hello', *('world', 000), **{'name': 'Test', 'age': 24})# Common args: hello# Single args: (’world’, 0)# Double args: {’name’: ’Test’, ’age’: 24}

到此這篇關(guān)于Python 帶星號(hào)(* 或 **)的函數(shù)參數(shù)詳解的文章就介紹到這了,更多相關(guān)Python 帶星號(hào)參數(shù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 措勤县| 合阳县| 乐亭县| 临潭县| 延寿县| 永春县| 察雅县| 如皋市| 阿拉善左旗| 资中县| 孝感市| 宁武县| 祁连县| 章丘市| 扎兰屯市| 广西| 日喀则市| 盐城市| 元朗区| 婺源县| 广元市| 兴和县| 全南县| 纳雍县| 晋州市| 四会市| 万州区| 葵青区| 托克托县| 麻阳| 合阳县| 咸宁市| 和平县| 江津市| 兰考县| 宝应县| 平南县| 曲阳县| 习水县| 仙桃市| 若尔盖县|