1. 帶默認值的參數
在了解帶星號(*)的參數之前,先看下帶有默認值的參數,函數定義如下:
>> def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
print("Common args", common)
print("Default String", defaultStr)
print("Default Number", defaultNum)
(1)帶默認值的參數(defaultStr、defaultNum)不傳參時的調用:
>> defaultValueArgs("Test")
Common args Test
Default String default
Default Number 0
(2)帶默認值的參數(defaultStr、defaultNum),調用的時候可以直接傳參(如下例中的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
注意:在函數定義時,第一個帶有默認值的參數之后的所有參數都必須有默認值,否則,運行時報錯。
>> 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.帶一個星號(*)的函數參數
帶一個參數的函數定義如下:
>> def singalStar(common, *rest):
print("Common args: ", common)
print("Rest args: ", rest)
(1)帶星號(*)的參數不傳參:
>> singalStar("hello")
Common args: hello
Rest args: ()
帶星號(*)的參數不傳參時默認是一個空的元組。
(2)帶星號(*)的參數傳入多個值時(個數大于或等于函數定義時的參數個數):
>> singalStar("hello", "world", 000)
Common args: hello
Rest args: ('world', 0)
不難看出,第二種方式中,星號參數把接收的多個參數合并為一個元組。
(3)當我們直接傳元組類型的值給星號參數時:
>> singalStar("hello", ("world", 000))
Common args: hello
Rest args: (('world', 0),)
此時,傳遞的元組值作為了星號參數的元組中的一個元素。
(4)如果我們想把元組作為星號參數的參數值,在元組值前加上" * " 即可。
>> 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.帶兩個星號(**)的函數參數
帶兩個星號(**)的函數定義如下:
>> def doubleStar(common, **double):
print("Common args: ", common)
print("Double args: ", double)
(1)雙星號(**)參數不傳值:
>> doubleStar("hello")
Common args: hello
Double args: {}
帶雙星號(**)的參數不傳值時默認是一個空的字典。
(2)雙星號(**)參數傳入多個參數時(個數大于或等于函數定義時的參數個數):
>> 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}
可以看到,雙星號參數把接收的多個參數合并為一個字典,但與單星號不同的是,此時必須采用默認值傳參的 “ args = value ” 的方式,“ = ” 前的字段成了字典的鍵,“ = ” 后的字段成了字典的值。
(3)如果想把字典作為星號參數的參數值,那么該怎么辦呢?與單星號參數類似,在字典值前加上 “ ** ”,同時其后不能添加任何值。
>> 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、在有些情況下,單星號函數參數和雙星號函數參數是一起使用的:
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}
到此這篇關于Python 帶星號(* 或 **)的函數參數詳解的文章就介紹到這了,更多相關Python 帶星號參數內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python函數參數中的*與**運算符
- 詳解Python 函數參數的拆解
- Python函數參數定義及傳遞方式解析
- python+opencv邊緣提取與各函數參數解析
- Python 限定函數參數的類型及默認值方式
- python函數參數(必須參數、可變參數、關鍵字參數)
- python 編碼中為什么要寫類型注解?
- 深入淺析Python 函數注解與匿名函數
- Python中typing模塊與類型注解的使用方法
- Python使用functools實現注解同步方法
- Python函數參數和注解的使用