'''函数的可变参数'''
def showmessage(name, *p_info):
print("name:", name)
for i in p_info:
print(i, end=",")
return
def showmoremessage(na, *p_in, **scores):
print("name:", na)
for i in p_in:
print(i, end=",")
for item in scores.items():
print(item, end=".....")
return
#调用showmessage函数
showmessage("dailijie")
showmessage("dailijie", "Lemon", 18, "like English")
print("--------------------------------")
showmoremessage("dailijie", "Lemon", 18, "want to be a designer", math = 100, English = 600)
以下是运行后的结果:
name:dailijie
name: dailijie
Lemon,18,like English,--------------------------------
name: dailijie
Lemon,18,want to be a designer,(‘math’, 100)…(‘English’, 600)…
从以上实例,我们可以看出,name是第一个形参,也就是说,在你调用showmessage()这个函数的时候,你传入的第一个实参就会和name这个形参相匹配,*p_info可表示元组类型的数据,即组合型数据,这个实参数可以是0(只传对应的name这一个实参)也可以是多个(如接下来的"Lemon", 18, “like Englis”)以元组的形式输出,在**scores这个形参中,是以字典的形式传入的(如math = 100, English = 600)
希望对你有所帮助!!!