3.8.0 Documentation https://docs.python.org/zh-cn/3/
python 官方文档
入门教程
20191209
4.7.5 解包参数列表
当参数已经在列表或元组中,但要为需要独立位置参数的函数调用解包时,会发生相反的情况,例如,内置的range()函数需要单独的start和stop参数。如果它们不能单独使用,可以使用“*”操作符来编写函数调用以便从列表或元组中解包参数:
# 解包参数列表*
print(list(range(3,6))) # normal call with separate arguments
args = [3,6]
print(list(range(*args))) # call with arguments unpacked from a list
# 字典用**操作符,来提供关键字参数
def parrot(voltage,state='a stiff',action='voom'): # voltage 电压
print("--This parrot wouldn't",action,end=' ') # 这只鹦鹉不会 action
print("if you put",voltage,'volts through it.',end =' ') # 如果你把voltage 伏特 通过它
print("E's",state,"!") # 现在的状态
d = {"voltage":"four million","state":"bleedin'demised","action":"VOOM"}
parrot(**d)
# 实现字典的合并
def merge_dict(dic1,dic2):
return {**dic1,**dic2}
dic1 = {'a':1,"b":2}
dic2 = {'c':3,"d":4}
print(merge_dict(dic1,dic2))
运行结果
[3, 4, 5]
[3, 4, 5]
–This parrot wouldn’t VOOM if you put four million volts through it. E’s bleedin’demised !
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}