python参数解包的实现

python参数解包的实现

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

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

>>>list(range(3,6))#normalcallwithseparatearguments

[3,4,5]

>>>args=[3,6]

>>>list(range(*args))#callwithargumentsunpackedfromalist

[3,4,5]

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

>>>defparrot(voltage,state='astiff',action='voom'):

...print("--Thisparrotwouldn't",action,end='')

...print("ifyouput",voltage,"voltsthroughit.",end='')

...print("E's",state,"!")

...

>>>d={"voltage":"fourmillion","state":"bleedin'demised","action":"VOOM"}

>>>parrot(**d)

以上就是python参数解包的实现,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

申请14天超长免费试听资格
获取500G教程资料
姓名
电话
课程
立即申请