- #带参数的函数,x、y形参,xxx、yyy实参
- def test(x,y,z=3):
- print (x)
- print (y)
- print (z)
- #位置参数调用,实参和形参要一一对应
- test('xxx','yyy')
- #关键字参数调用,与形参顺序无关
- test(y=2,x=1)
- #默认参数
- #默认参数特点:调用函数的时候,默认参数非必须传递
- #用途:
- def test(x,y=2):
- print (x)
- print (y)
- test(1)
- test(1,3)
- def test(*args):
- print (args)
- test(1,2,3,4,5)
- test(*[1,2,3,4,5]) #*args = *[1,2,3,4,5] args=tuple([1,2,3,4,5])
- l = [1,2,3]
- test(*l)
- def test1(x,*args):
- print (x)
- print (args)
- test1(1,2,3,4,5,6,7)
- #**kwargs,把n个关键字参数,转换成字典的方式
- def test2(**kwargs):
- print (kwargs)
- test2(name='jiachen',age=27)
- d = {'name':'jiachen','age':27}
- test2(**d)
- def test3(x,*args,**kwargs):
- print (x)
- print (args)
- print (kwargs)
- test3(1,*[2,3,4],**{'name':'jiachen','age':27})
来源: