欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年02月23日 10:02:33   作者:Jianzhi.Z  
這篇文章主要介紹了Python 帶星號(hào)(* 或 **)的函數(shù)參數(shù)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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 Test
Default String default
Default Number 0
 

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

>> defaultValueArgs("Test", "Str", defaultNum = 1)
 
Common args Test
Default String Str
Default Number 1
 
>> defaultValueArgs("Test", defaultNum = 1)
 
Common args Test
Default String default
Default 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: hello
Rest 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: hello
Rest args: ('world', 0)

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

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

>> singalStar("hello", ("world", 000))
 
Common args: hello
Rest args: (('world', 0),)

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

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

>> singalStar("hello", *("world", 000))
Common args: hello
Rest args: ('world', 0)

>> singalStar("hello", *("world", 000), "123")
Common args: hello
Rest 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: hello
Double 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: hello
Double 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: hello
Double 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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)漢諾塔方法匯總

    python實(shí)現(xiàn)漢諾塔方法匯總

    本文給大家匯總了幾種使用Python結(jié)合遞歸算法實(shí)現(xiàn)漢諾塔的方法,非常的簡(jiǎn)單實(shí)用,對(duì)大家學(xué)習(xí)Python很有幫助,希望大家能夠喜歡
    2016-07-07
  • 創(chuàng)建Shapefile文件并寫入數(shù)據(jù)的例子

    創(chuàng)建Shapefile文件并寫入數(shù)據(jù)的例子

    今天小編就為大家分享一篇?jiǎng)?chuàng)建Shapefile文件并寫入數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 如何在VSCode上輕松舒適的配置Python的方法步驟

    如何在VSCode上輕松舒適的配置Python的方法步驟

    這篇文章主要介紹了如何在VSCode上輕松舒適的配置Python的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Django項(xiàng)目創(chuàng)建到啟動(dòng)詳解(最全最詳細(xì))

    Django項(xiàng)目創(chuàng)建到啟動(dòng)詳解(最全最詳細(xì))

    這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目創(chuàng)建到啟動(dòng)的步驟,本文介紹的方法算是最全最詳細(xì)的一個(gè)項(xiàng)目,需要的朋友可以參考下
    2019-09-09
  • Python常見(jiàn)沙箱技術(shù)與沙箱逃逸避免方法詳解

    Python常見(jiàn)沙箱技術(shù)與沙箱逃逸避免方法詳解

    Python沙箱可以幫助你在安全的環(huán)境中運(yùn)行不受信任的代碼,本文將探討?Python?沙箱的概念、常見(jiàn)的沙箱技術(shù)以及如何避免沙箱逃逸,感興趣的可以了解下
    2024-01-01
  • python3的pip路徑在哪

    python3的pip路徑在哪

    在本篇文章里小編給大家分享的是關(guān)于python3中pip路徑位置的相關(guān)文章,有興趣的朋友們學(xué)習(xí)下吧。
    2020-06-06
  • python 循環(huán)while和for in簡(jiǎn)單實(shí)例

    python 循環(huán)while和for in簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇python 循環(huán)while和for in簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • Python爬蟲入門案例之爬取二手房源數(shù)據(jù)

    Python爬蟲入門案例之爬取二手房源數(shù)據(jù)

    讀萬(wàn)卷書不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),今天小編給大家?guī)?lái)一份python爬取二手房源信息的案例,可以用來(lái)直觀的了解房?jī)r(jià)行情,大家可以在過(guò)程中查缺補(bǔ)漏,看看自己掌握程度怎么樣
    2021-10-10
  • Python與數(shù)據(jù)庫(kù)的交互問(wèn)題小結(jié)

    Python與數(shù)據(jù)庫(kù)的交互問(wèn)題小結(jié)

    這篇文章主要介紹了Python與數(shù)據(jù)庫(kù)的交互,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • 詳解python3中的真值測(cè)試

    詳解python3中的真值測(cè)試

    這篇文章主要介紹了詳解python3中的真值測(cè)試,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08

最新評(píng)論