python基础5:函数参数传递方式

python3 函数的参数传递方式

  1. 位置传参
  2. 序列传参
  3. 关键字传参
  4. 字典关键字传参
  5. 综合传参

1、位置传参

实际调用参数(实参)的对应关系与形式参数(形参)的对应关系是按位置依次对应传入。

def infoStudent(name,age,addr):
    return '%s \'s age is %d and he comes from %s' %(name,age,addr)
print(infoStudent('Mike',23,'Beijing'))     # 按位置依次传参
Output:
Mike 's age is 23 and he comes from Beijing

2、序列传参

序列传参是指在函数调用过程中,用 星号* 将序列(tuple or list)拆解后按位置进行传递的传参方式,实参和形参通过序列传递和匹配。

def infoStudent(name,age,addr):
    return '%s \'s age is %d and she comes from %s' %(name,age,addr)
L = ['Timy',23,'Wuhan'] # 参数列表: a list
print(infoStudent(*L))      # 传参: *L   (*L: 将L按顺序拆解后依次对应传入infoStudent函数中)
Output:
Timy 's age is 23 and she comes from Wuhan

3、关键字传参

是指传参时, 按形参的名称给形参赋值,实参和形参按名称进行对应匹配传入。

def infoStudent(name,age,addr):
    return '%s \'s age is %d and she comes from %s' %(name,age,addr)
# 实参和形参按形参名进行匹配, 可以不按位置进行匹配
print(infoStudent(age=16, name='Lucy',addr='Guangzhou'))

Output:
Lucy 's age is 16 and she comes from Guangzhou

4、字典关键字传参

实参为字典, 用 双星号** 拆解字典后再按关键字传参方式进行传入。

def infoStudent(name,age,addr):
    return '%s \'s age is %d and she comes from %s' %(name,age,addr)
print(infoStudent(**{'age':16, 'name':'Lucy','addr':'Guangzhou'}))
# dict参数前一定要有 ** ;字典内元素的可以无序,会按key进行关键字匹配并传入value
dict_arg = {'addr':'Guangzhou','age':16, 'name':'Lucy'}
print(infoStudent(**dict_arg))
Output:
Lucy 's age is 16 and she comes from Guangzhou
Lucy 's age is 16 and she comes from Guangzhou
def infoStudent(name,age,addr,classID='A-1003'):
    return '%s \'s age is %d and comes from %s. In %s' %(name,age,addr,classID)
dict_arg = {'addr':'Guangzhou','age':16, 'name':'Lucy'}
# dict_arg中没有calssID实参,缺省实参自动以缺省形参的默认值传入
print(infoStudent(**dict_arg))
Output:
Lucy 's age is 16 and she comes from Guangzhou. In A-1003

备注:

  • 字典的键名和形参名必须一致
  • 字典的键名必须为字符串
  • 字典的键名要在形参中存在

5、综合传参

函数的传参方式在能确定形参能唯一匹配到相应实参的情况下可以任意组合。通常位置传参和序列传参先传递, 其次是关键字传参和字典传参

def infoStudent(name,age,addr,sex,rate,grade,classID='A-1003'):
    return '%s \'s age is %d,%s, ranking Top%d,grade is %d and she comes from %s. In %s' \
           %(name,age,sex,rate,grade,addr,classID)
print(infoStudent('Juney',classID='A-1220', *(25,'XFN'),sex=20,**{'rate':2,'grade':99}))

Output:
Juney 's age is 25,20, ranking Top2,grade is 99 and she comes from XFN. In A-1220

补充:
函数参数自左至右的顺序为:

 1. positional argument  位置参数
 2. keyword argument 关键字参数(包括缺省参数)
 3. iterable argument unpacking  序列参数/星号元组参数 *args
 4. keyword argument unpacking  字典关键字参数/双星号字典参数 **kwars

注意:

 1. positional argument 只能在最左位置
 2.  *args 只能在 **kwars前面的位置
 3.  keyword argument在positional argument之后的任意位置均可
def infoStudent(name,age,addr,sex,rate,grade,classID='A-1003'):
    return '%s \'s age is %d,%s, ranking Top%d,grade is %d and she comes from %s. In %s' %(name,age,sex,rate,grade,addr,classID)
# 默认值传入
print(infoStudent('Juney',*(25,'XFN'),sex=20,**{'rate':2,'grade':99}))
# 改变传参顺序
print(infoStudent('Juney',sex=20,*(25,'XFN'),**{'rate':2,'grade':99}))
print(infoStudent('Juney',classID='A-1220', *(25,'XFN'),sex=20,**{'rate':2,'grade':99}))
print(infoStudent('Juney',*(25,'XFN'),sex=20,classID='A-1220',**{'rate':2,'grade':99}))
print(infoStudent('Juney',classID='A-1220', *(25,'XFN'),**{'rate':2,'grade':99},sex=20))

Output:
Juney 's age is 25,20, ranking Top2,grade is 99 and she comes from XFN. In A-1220

另:
可以接收任意位置传参和关键字传参的函数:

def fn(*args, **kwars):
    pass
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章