python---函数
#1.位置参数和关键字参数def test1(x,y,z):
print(x)
print(y)
print(z)
test1(1,2,3) #位置参数,与顺序有关
test1(y=2,z=3,x=1)#关键字参数,与位置无关
test1(1,z=3,y=2) #既有位置参数,又有关键字参数,位置参数不能再关键字参数前面
#2.默认参数
def test2(x,y=2):
print(x)
print(y)
test2(1) #如果不指定y,则使用默认值,指定新值则用新值
test2(1,3)
test2(1,y=3)
#3.参数组
def test3(*args): #将传入值转化成一个元组
print(args)
test3(1,2,3,4,5)
test3(*)
#输出
# (1, 2, 3, 4, 5)
# (1, 2, 3, 4, 5)
def test4(x,*args):
print(x)
print(args)
test4(1,2,3,4,5)
#输出
# 1
# (2, 3, 4, 5)
def test5(**kwargs): #将“关键字参数”转化成一个字典
print(kwargs)
test5(y=1,x=2,z=3)
#输出
# {'y': 1, 'x': 2, 'z': 3}
def test6(name,age=18,**kwargs):
print(name)
print(age)
print(kwargs)
test6("feng",y=1,x=2)
#输出
# feng
# 18
# {'y': 1, 'x': 2}
test6("feng",23,y=1,x=2)
#输出
# feng
# 23
# {'x': 2, 'y': 1}
页:
[1]