函数参数*arg和参数**arg区别

本文介绍了Python中定义函数时使用*args和**kwargs的方法。通过示例展示了如何利用*args接收任意数量的位置参数,并将它们存储为元组。同时,解释了如何通过**kwargs接收关键字参数并将它们存储为字典。
def sandwich(*toppings):
print("\nMaking a sandwich with the following toppings: ")
print(toppings)
for topping in toppings:
print(topping)
sandwich('banna','apple')
sandwich('apple','beef','pairs')
sandwich('apple')

参数*toppings意思是创建了一个名为toppings的元组,可以通过遍历循环将其打印出来。并且它可以接纳任意数量实参的形参,这种形参都放在函数参数的最后。


def car(make_bussines,type_car,**user_info):
car = {}
car['make_bussines'] = make_bussines
car['type_car'] = type_car
for key,value in user_info.items():
car[key] = value
return car
make_car = car('subaru','outback',color = 'blue',tow_package = 'True')

print(make_car)

而**user_info则是创建了一个字典,传参的时候必须是

make_car = car('subaru','outback',color = 'blue',tow_package = 'True')

键-值对应传递

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值