将键值对当作实参传进函数
生活随笔
收集整理的這篇文章主要介紹了
将键值对当作实参传进函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
def build_profile(first,last,**user_info):
? ? profile={}
? ? profile['firstname']=first
? ? profile['lastname']=last
? ? for k,v in user_info.items():
? ? ? ? profile[k]=v
? ? return profile
user_profile=build_profile('yuanqing','li','place'='chengdu',school='uestc')
? ? profile={}
? ? profile['firstname']=first
? ? profile['lastname']=last
? ? for k,v in user_info.items():
? ? ? ? profile[k]=v
? ? return profile
user_profile=build_profile('yuanqing','li','place'='chengdu',school='uestc')
print(user_profile)
報錯:
File "<ipython-input-7-78b8969575f2>", line 9 user_profile=build_profile('yuanqing','li','place'='chengdu',school='uestc') ^SyntaxError: keyword can't be an expression
將鍵值對傳進函數(shù)作為實參傳進函數(shù)實,不能寫'place'='chengdu',應該寫place='chengdu',具體理由現(xiàn)在還不清楚,后面來補充。
補充:在操作字典時,鍵確實需要加‘’,但是此處應該遵守關鍵字實參的傳遞規(guī)則,關鍵字不能是expression。
總結
以上是生活随笔為你收集整理的将键值对当作实参传进函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java学习与总结:索引
- 下一篇: 关于for循环处理列表的思考