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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章