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 sqlobject(mysql)中文乱码解决方法
Nov 14 Python
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
Jul 06 Python
zookeeper python接口实例详解
Jan 18 Python
详解Python下Flask-ApScheduler快速指南
Nov 04 Python
总结python中pass的作用
Feb 27 Python
Python I/O与进程的详细讲解
Mar 08 Python
对于Python深浅拷贝的理解
Jul 29 Python
Python字符串格式化输出代码实例
Nov 22 Python
用python绘制樱花树
Oct 09 Python
浅谈anaconda python 版本对应关系
Oct 07 Python
python matplotlib工具栏源码探析三之添加、删除自定义工具项的案例详解
Feb 25 Python
忆童年!用Python实现愤怒的小鸟游戏
Jun 07 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自带ZIP压缩、解压缩类ZipArchiv使用指南
2015/03/03 PHP
了解PHP的返回引用和局部静态变量
2015/06/04 PHP
CI配置多数据库访问的方法
2016/03/28 PHP
Javascript学习笔记2 函数
2010/01/11 Javascript
GWT中复制到剪贴板 js+flash实现复制 兼容性比较好
2010/03/07 Javascript
利用CSS、JavaScript及Ajax实现高效的图片预加载
2013/10/16 Javascript
JS弹出层的显示与隐藏示例代码
2013/12/27 Javascript
php析构函数的具体用法小结
2014/03/11 Javascript
Js冒泡事件详解及阻止示例
2014/03/21 Javascript
jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
2015/03/24 Javascript
最精简的JavaScript实现鼠标拖动效果的方法
2015/05/11 Javascript
jquery checkbox的相关操作总结
2016/10/17 Javascript
js实现功能比较全面的全选和多选
2017/03/02 Javascript
Vue原理剖析 实现双向绑定MVVM
2017/05/03 Javascript
JS实现闭包中的沙箱模式示例
2017/09/07 Javascript
浅析Node.js非对称加密方法
2018/01/29 Javascript
AngularJS ui-router刷新子页面路由的方法
2018/07/23 Javascript
浅谈发布订阅模式与观察者模式
2019/04/09 Javascript
Vue中el-form标签中的自定义el-select下拉框标签功能
2020/04/20 Javascript
Python中输出ASCII大文字、艺术字、字符字小技巧
2015/04/28 Python
利用Python自动监控网站并发送邮件告警的方法
2016/08/24 Python
使用Python从零开始撸一个区块链
2018/03/14 Python
如何优雅地改进Django中的模板碎片缓存详解
2018/07/04 Python
python 判断矩阵中每行非零个数的方法
2019/01/26 Python
计算机二级python学习教程(1) 教大家如何学习python
2019/05/16 Python
python 工具 字符串转numpy浮点数组的实现
2020/03/14 Python
Anaconda详细安装步骤图文教程
2020/11/12 Python
解决TensorFlow训练模型及保存数量限制的问题
2021/03/03 Python
CSS3 简写animation
2012/05/10 HTML / CSS
台湾乐天市场:日本No.1的网路购物网站
2017/03/22 全球购物
国家地理在线商店:Shop National Geographic
2018/06/30 全球购物
优秀导游先进事迹材料
2014/01/25 职场文书
借款担保书范文
2014/05/13 职场文书
推广活动策划方案
2014/08/23 职场文书
绿色环保家庭事迹材料
2014/08/31 职场文书
英文升职感谢信
2015/01/23 职场文书