python 拆包装包_python的包装和拆包

包装和拆包

下面是一个显示包装和拆包的示例,这里**解包了与它一起使用的字典,并将字典中的项作为关键字参数传递给函数。所以写“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,太棒了,世界!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值