包装和拆包
下面是一个显示包装和拆包的示例,这里**解包了与它一起使用的字典,并将字典中的项作为关键字参数传递给函数。所以写“fun(1,** d)”相当于写“fun(1,b = 4,c = 10)”。
# A Python program to demonstrate both packing and
# unpacking.
# A sample python function that takes three arguments
# and prints them
def fun1(a, b, c):
print(a, b, c)
# Another sample function.
# This is an example of PACKING. All arguments passed
# to fun2 are packed into tuple *args.
def fun2(*args):
# Convert args tuple to a list so we can modify it
args = list(args)
# Modifying args
args[0] = 'Geeksforgeeks'
args[1] = 'awesome'
# UNPACKING args and calling fun1()
fun1(*args)
# Driver code
fun2('Hello', 'beautiful', 'world!')
输出:(Geeksforgeeks,太棒了,世界!)