Python带星号*的函数参数

您所在的位置:网站首页 python中自定义函数至少需要一个参数吗对吗 Python带星号*的函数参数

Python带星号*的函数参数

2024-07-10 23:36:33| 来源: 网络整理| 查看: 265

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.总结

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

完整的代码如下:

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})



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭