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}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“Python 带星号(* 或 **)的函数参数详解”评论...
更新动态
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]