您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 西双版纳分类信息网,免费分类信息发布

Python函数学习之参数列表解包

2023/7/16 15:17:03发布37次查看
the reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. for instance, the built-in range() function expects separate start and stop arguments. if they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:当参数已经在列表或元组中时,相反的情况发生,但需要为需要单独位置参数的函数调用解包。例如,内置的range()函数需要单独的开始和停止参数。如果它们单独不可用,则用*-运算符编写函数调用,以从列表或元组中解开参数:
>>> 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]
in the same fashion, dictionaries can deliver keyword arguments with the **-operator:以同样的方式,字典可以用**运算符传递关键字参数 即**-操作符来解包参数:
>>> 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)
-- this parrot wouldn't voom if you put four million volts through it. e's bleedin' demised !
西双版纳分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录