知行编程网知行编程网  2023-01-12 23:00 知行编程网 隐藏边栏  0 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python参数解包的实现的相关知识,希望可以帮到处于编程学习途中的小伙伴

python参数解包的实现

在参数的使用中,还有一种解包情况是我们需要掌握的。例如,将列表或字典的值转换成函数的参数,就需要用到参数拆包的功能。


1、* 操作符 可以用来解包列表和元组。

>>> list(range(3, 6))            # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))            # call with arguments unpacked from a list
[3, 4, 5]


2、** 操作符 可以用来解包字典。

>>> def parrot(voltage, state='a stiff', action='voom'):
...     print("-- This parrot wouldn't", action, end=' ')
...     print("if you put", voltage, "volts through it.", end=' ')
...     print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)


以上就是python参数解包的实现,希望能对大家有所帮助。

更多Python学习指路:


本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享