创新互联Python教程:python参数解包的实现

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

成都创新互联公司专业为企业提供香洲网站建设、香洲做网站、香洲网站设计、香洲网站制作等企业网站建设、网页设计与制作、香洲企业网站模板建站服务,10年香洲做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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学习指路:创新互联python教程

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


分享题目:创新互联Python教程:python参数解包的实现
链接分享:http://csdahua.cn/article/dpcsgei.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流