Python 函数参数详解:默认值、单星号与双星号用法
本文详细解析 Python 函数中三种核心参数类型:带有默认值的普通参数、使用单星号 (*) 收集多余位置参数的 *args,以及使用双星号 (**) 收集关键字参数的 **kwargs。通过具体代码示例,说明它们各自的定义规则、调用方式及组合使用时的注意事项,帮助开发者掌握 Python 函数参数的灵活用法。
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),也可以使用“参数名=值”的形式(例如指定 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: ()
此时,该星号参数默认接收一个空的元组(tuple)。
(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: {}
此时,该双星号参数默认接收一个空的字典(dict)。
(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}
可以看到,双星号参数会将接收到的多个命名参数合并为一个字典。与单星号不同的是,这里必须采用“参数名=值”的形式进行传递,“=”前的字段成为字典的键,“=”后的字段成为字典的值。
(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 相关技巧,可查阅本站往期文章或继续浏览后续内容,感谢支持!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









