Python 带星号(* 或 **)的函数参数详解

2022-07-24,,,,

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 带星号(* 或 **)的函数参数详解.doc》

下载本文的Word格式文档,以方便收藏与打印。