有关python 函数参数
# def foo(x):
# print(x)
#
#
# foo(1)
# foo('a')
# foo({'a':2})
#形参与实参:
# def foo(x,y): #x=1,y=2
# return x+y
#
# foo(1,2)
#===================================part1
#位置参数:按照从左到右的顺序依次定义的参数
def foo(x,y):
print(x)
print(y)
#按位置定义的形参,必须被传值,多一个不行,少一个也不行
# foo(1,2,3)
#按位置定义的实参,与形参一一对应
# foo(2,10)
#===================================part2
#关键字参数:实参在定义时,按照key=value形式定义
# def foo(x,y):
# print(x)
# print(y)
#
# # foo(y=10,x=1)
# foo(y=10,x=1) #关键字参数可以不用像位置实参一样与形参一一对应,指名道姓地传值
def foo(x,y):
print(x)
print(y)
# foo(1,10)
# foo(x=1,y=10)
# foo(1,y=10)
# foo(y=10,1)
# foo(x=10,1)
# foo(1,x=10,y=20)
# def foo(x,y,z):
# print(x)
# print(y)
# print(z)
# # foo(1,z=20,10)
# foo(1,y=2,z=10)
#注意的问题一:位置实参必须在关键字实参的前面
#注意的问题二:实参的形式既可以用位置实参又可以是关键字实参,但是一个形参不能重复传值
#===================================part3
#默认参数:在定义函数阶段,就已经为形参赋值,定义阶段有值,调用阶段可以不用传值
# def func(x,y=10):
# print(x)
# print(y)
# func(1,20)
# func(1)
# def func(y=10,x):
# print(x)
# print(y)
#位置实参
#关键字实参
#位置形参
#默认参数(形参)
#形参的应用:值经常变化的需要定义成位置形参,
# 值大多数情况下都一样,需要定义成默认参数
# def register(name,age,sex='male'):
# print(name,age,sex)
#
# register('alex',1000)
# register('wupeiqi',9000)
# register('yuanhao',10000)
# register('王铁蛋',10000,'female')
#默认参数需要注意的问题一:必须放在位置形参后面
#默认参数需要注意的问题二:默认参数通常要定义成不可变类型
#默认参数需要注意的问题三:默认参数只在定义阶段被赋值一次
# x=10
# def func(name,age=x):
# print(name)
# print(age)
# x=20
#
# func('egon')
# def func(name,age=10,sex='male',hobby=['play1','play2']):
# print(name)
# print(age)
# print(hobby)
#
# func('egon')
#===================================part4
#可变长参数:可变长指的是实参的个数不固定
#按位置定义的可变长度的实参:*
#按关键字定义的可变长度的实参:**
#
# def func(x,y,*args): #x=1,y=2,args=(3,4,5,6)
# print(x,y)
# print(args)
#
# func(1,2,3,4,5,6)
# def func(x,y,*args): #args=(3,4,5,6)
# print(x,y)
# print(args)
#
# func(1,2,*(3,4,5,6)) #foo(1,2,3,4,5,6)
# def func(x,y,z):
# print(x,y,z)
#
# # func(1,*(2,3)) #func(1,2,3)
# func(*(2,3)) #func(2,3)
#
# def func(x,y=2,z=1):
# print(x,y,z)
#
#
# func(*('a','b','c')) #func('a','b','c')
# def func(x,y,**kwargs): #x=1,y=2,kwargs={'a':1,'b':3,'z':3}
# print(x,y)
# print(kwargs)
#
# func(1,y=2,z=3,a=1,b=3)
# def func(x,y,**kwargs): #x=1,y=2,**kwargs=**{'a':1,'b':3,'z':3}
# print(x,y)
# print(kwargs)
#
# func(1,y=2,**{'a':1,'b':3,'z':3}) #func(1,y=2,z=3,b=3,a=1)
# def func(x,y=1,z=1):
# print(x,y,z)
#
# func(**{'y':2,'x':1,'z':3}) #
# def wrapper(*args,**kwargs): #可以接受任意形式,任意长度的参数
# print(args)
# print(kwargs)
#
#
# wrapper(1,2,3,3,3,3,3,x=1,y=2,z=3)
#
# def index(name,group):
# print('welcome %s to index page,group is: %s' %(name,group))
#
# def wrapper(*args,**kwargs): #可以接受任意形式,任意长度的参数
# # print(args) #args=(1,2,3)
# # print(kwargs) #kwargs={'x':1,'y':2,'z':3}
# index(*args,**kwargs) #index(*(1,2,3),**{'x':1,'y':2,'z':3}) #index(1,2,3,z=3,y=2,x=1)
#
# wrapper(name='egon',group='group1')
#命名关键字参数:定义在*后的形参,这类形参,必须被传值,而且要求实参必须是以关键字的形式来传值
# def register(**kwargs):
# print(kwargs)
# if 'name' in kwargs:
# print(kwargs['name'])
# if 'age' in kwargs:
# print(kwargs['age'])
#
#
# # register(name='egon',age=18)
# register()
# def register(*args,name='egon',age):
# print(args)
# print(name)
# print(age)
#
# # register(name='egon',age=18)
# register(1,2,2,3,age=10)
#
# def register(name,age,*,group,**kwargs):
# print(name)
# print(age)
# print(kwargs)
#
#
# # register('egon',18)
# register('egon1',18,hobby='paly1',group='group1')
# def func(x,y=1,*args,z,**kwargs):
# print(x)
# print(y)
# print(args)
# print(z)
# print(kwargs)
#
# func(1,2,3,4,5,z=10,a=1,b=2)
#
# def func(x,*args,z=1,**kwargs):
# print(x)
# print(args)
# print(z)
# print(kwargs)
#
# func(1,2,3,4,5,a=1,b=2,c=3)
#形参:位置形参,默认参数,*args,命名关键字参数,**kwargs
# def foo(x,y=1,*args,z,**kwargs):
# print(x)
# print(y)
# print(args)
# print(z)
# print(kwargs)
# foo(1,2,3,4,5,z=10,a=1,b=3)