Python带星号*的函数参数

1.带默认值的参数

在了解带星号(*)的参数之前,先看下带有默认值的参数,函数定义如下:

def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum)   

带默认值的参数不传参时的调用:

defaultValueArgs("Test")

#Common args Test
#Default String default
#Default Number 0

带默认值的参数,调用的时候可以直接传参,也可以写成“argsName = value”的形式:

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

 
注意:在函数定义时,第一个带有默认值的参数之后的所有参数都必须有默认值,否则,运行时报错。 

2.带一个星号(*)的函数参数

带一个参数的函数定义如下:

def singalStar(common, *rest):
    print("Common args: ", common)
    print("Rest args: ", rest)

第一种方式,星号(*)参数不传参:

singalStar("hello")

#Common args:  hello
#Rest args:  ()

第二种方式,传多个值(个数大于或等于函数定义时的参数个数):

singalStar("hello", "world", 000)

#Common args:  hello
#Rest args:  ('world', 0)

不难看出,上述方式中,星号参数把接收的参数合并为一个元组。

第三种方式,竟然星号参数把接收的参数作为元组,那么我们直接传元组类型的值:

singalStar("hello", ("world", 000))

#Common args:  hello
#Rest args:  (('world', 0),)

没错,你没看错,传递的元组值作为了星号参数的元组中的一个元素。

第四种方式,但是有时候我们想把元组值就作为星号参数的参数值,那么该怎么办呢?好办,在元组值前加上“*”即可,不过此时,就不能在加了“*”的元组后,追加任何值了。

singalStar("hello", *("world", 000))
#     singalStar("hello", *("world", 000), "123")    #error

#Common args:  hello
#Rest args:  ('world', 0)

3.带两个星号(*)的函数参数

带两个星号(*)的函数定义如下:

def doubleStar(common, **double):
    print("Common args: ", common)
    print("Double args: ", double)

第一种方式,星号(*)参数不传值:

doubleStar("hello")

#Common args:  hello
#Double args:  {}

第二种方式,传多个参数(个数大于或等于函数定义时的参数个数)。但是,这和单星号参数传值方式肯定不一样,否则,不就乱套了吗。

doubleStar("hello", "Test", 24)       #error
doubleStar("hello", x = "Test", y = 24)

#Common args:  hello
#Double args:  {'y': 24, 'x': 'Test'}

不难发现,此时必须采用第一节的默认值传参的“args = value”的方式。同时,“=”前的字段成了字典的键,“=”后的字段成了字典的值。即,双星号参数接收的参数作为字典。

第三种方式,有时候我们想把字典值就作为星号参数的参数值,那么该怎么办呢?同单星号参数,在字典值前加上“**”,同时其后不能添加任何值。

#doubleStar("hello", **{"name": "Test", "age": 24}, {"name": "Test2", "age": 24})    #error
#doubleStar("hello", {"name": "Test", "age": 24})    #error
doubleStar("hello", **{"name": "Test", "age": 24})

#Common args:  hello
#Double args:  {'name': 'Test', 'age': 24}

在有些情况下,单星号函数参数和双星号函数参数是一起使用的,定义如下:

def singalAndDoubleStar(common, *single, **double):
    print("Common args: ", common)
    print("Single args: ", single)
    print("Double args: ", double)

4.总结

  1. 默认值函数参数。这种函数定义时,第一个有默认值的参数后的每一个参数都必须提供默认值。传参时,可以直接传参,也可以以“默认值参数名=value”的形式传参。
  2. 单星号函数参数。单星号函数参数接收的参数组成一个元组。
  3. 双星号函数参数。双星号函数参数接收的参数组成一个字典。

完整的代码如下:

def singalStar(common, *rest):
    print("Common args: ", common)
    print("Rest args: ", rest)
    
def doubleStar(common, **double):
    print("Common args: ", common)
    print("Double args: ", double)
    
def singalAndDoubleStar(common, *single, **double):
    print("Common args: ", common)
    print("Single args: ", single)
    print("Double args: ", double)

def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum)   

if __name__ == "__main__":
    defaultValueArgs("Test")
    defaultValueArgs("Test", "default", defaultNum = 1)
     
    singalStar("hello")
    singalStar("hello", "world", 000)
    singalStar("hello", ("world", 000))
    singalStar("hello", ("world", 000), {123: 123})
    singalStar("hello", *("world", 000))
#     singalStar("hello", *("world", 000), "123")    #error
    
    doubleStar("hello")
    doubleStar("hello", x = "Test", y = 24)
    doubleStar("hello", **{"name": "Test", "age": 24})
#     doubleStar("hello", {"name": "Test", "age": 24})    #error

    singalAndDoubleStar("hello")
    singalAndDoubleStar("hello", "world", 000)
    singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})
    singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})
    singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})
#     singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24})      #error
    singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})


  • 51
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值