python_传递任意数量的实参

'''
def name(*args): #python创建一个空元组,将收到的所有值都封装在这个元组中
"""打印所有姓名"""
for info in args: #循环遍历args中的值
print(info)

name("a","b","c")
'''

'''
#结合使用位置实参和任意数量的实参
def make_pizza(size,*toppings):
print("pizza的尺寸为:"+str(size),"toppings为:")
for topping in toppings:
print("-"+topping)

make_pizza(16,"apple","orange","banana") #将接收到的第一个值存储在形参size中,其他值存放在元组topping中
'''

#使用任意数量的关键字实参

def build_profile(first,last,**user_info): #姓、名、等其他信息 **user_info创建一个空字典,并将收到的信息封装到字典中
"""创建一个字典,其中包含我们知道的有关用户的一切"""
profile = {} #创建一个空字典,用于存储用户简介
profile["first_name"] = first #将姓、名加入到字典中。存储first_name时,使用的参数是‘first’
profile["last_name"] = last
for key,value in user_info.items(): #遍历user_info中的键——值对,并将每个键——值加入到字典profile中
profile[key]=value
return profile #将回调字典profile返回给函数调用行

user_profile =build_profile('albert','lee',location='Nanjing',filed = 'python')
print(user_profile)

原文地址:https://www.cnblogs.com/monica001/p/10490739.html