导语:
本文主要介绍了关于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电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python2.7是否支持pyqt5?11/30
- ♥ 如何使用 python 随机模块中的 randint() 函数?08/18
- ♥ 如何优化python中请求的接口调用10/28
- ♥ 如何用python3.5打代码11/30
- ♥ c如何与python交互12/21
- ♥ 如何在python中写入随机数12/11
内容反馈