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


Posted in Python onFebruary 23, 2021

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 相关文章推荐
python的id()函数解密过程
Dec 25 Python
利用Python中的输入和输出功能进行读取和写入的教程
Apr 14 Python
20招让你的Python飞起来!
Sep 27 Python
Python获取SQLite查询结果表列名的方法
Jun 21 Python
Python3学习笔记之列表方法示例详解
Oct 06 Python
Python2实现的图片文本识别功能详解
Jul 11 Python
python单例模式获取IP代理的方法详解
Sep 13 Python
Django migrations 默认目录修改的方法教程
Sep 28 Python
Django如何自定义model创建数据库索引的顺序
Jun 20 Python
Python高级编程之继承问题详解(super与mro)
Nov 19 Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 Python
运行Python编写的程序方法实例
Oct 21 Python
python解决OpenCV在读取显示图片的时候闪退的问题
Feb 23 #Python
关于探究python中sys.argv时遇到的问题详解
Feb 23 #Python
python链表类中获取元素实例方法
Feb 23 #Python
Python之多进程与多线程的使用
Feb 23 #Python
Python绘制词云图之可视化神器pyecharts的方法
Feb 23 #Python
matplotlib grid()设置网格线外观的实现
Feb 22 #Python
浅析python连接数据库的重要事项
Feb 22 #Python
You might like
php中的boolean(布尔)类型详解
2013/10/28 PHP
CI框架Session.php源码分析
2014/11/03 PHP
PHP中把有符号整型转换为无符号整型方法
2015/05/27 PHP
php+redis在实际项目中HTTP 500: Internal Server Error故障排除
2017/02/05 PHP
详解使用php调用微信接口上传永久素材
2017/04/11 PHP
PHP-CGI远程代码执行漏洞分析与防范
2017/05/07 PHP
PHP PDOStatement::errorCode讲解
2019/01/31 PHP
PHP中的访问修饰符简单比较
2019/02/02 PHP
使用PHP反射机制来构造"CREATE TABLE"的sql语句
2019/03/21 PHP
PHP防止sql注入小技巧之sql预处理原理与实现方法分析
2019/12/13 PHP
ExtJS TabPanel beforeremove beforeclose使用说明
2010/03/31 Javascript
JS判断文本框内容改变事件的简单实例
2014/03/07 Javascript
NodeJS使用jQuery选择器操作DOM
2015/02/13 NodeJs
js实现鼠标移到链接文字弹出一个提示层的方法
2015/05/11 Javascript
聊一聊Vue.js过渡效果
2016/09/07 Javascript
js初始化验证实例详解
2016/11/26 Javascript
利用types增强vscode中js代码提示功能详解
2017/07/07 Javascript
Vue实现一个无限加载列表功能
2018/11/13 Javascript
vue如何在项目中调用腾讯云的滑动验证码
2020/07/15 Javascript
[47:35]VP vs Pain 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/20 DOTA
简单介绍利用TK在Python下进行GUI编程的教程
2015/04/13 Python
详解Python3中yield生成器的用法
2015/08/20 Python
Python利用公共键如何对字典列表进行排序详解
2018/05/19 Python
python SVM 线性分类模型的实现
2019/07/19 Python
Flask框架模板继承实现方法分析
2019/07/31 Python
python 读取更新中的log 或其它文本方式
2019/12/24 Python
tensorflow mnist 数据加载实现并画图效果
2020/02/05 Python
CSS3属性background-size使用指南
2014/12/09 HTML / CSS
CSS3中使用RGBa来调节透明度的教程
2016/05/09 HTML / CSS
荷兰和比利时时尚鞋店:Van Dalen
2018/04/23 全球购物
视图的作用
2014/12/19 面试题
禁烟标语大全
2014/06/11 职场文书
2014年酒店前台工作总结
2014/11/14 职场文书
先进基层党组织材料
2014/12/25 职场文书
励志语录:时光飞逝,请学会珍惜所有的人和事
2020/01/16 职场文书
Java比较两个对象中全部属性值是否相等的方法
2021/08/07 Java/Android