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