2010-11-14 59 views
4

因此,我在使用的API的Web POST請求中預期的可選參數實際上也是python中的保留字。那麼,如何命名帕拉姆在我的方法調用:Python命名的參數是關鍵字?

example.webrequest(x=1,y=1,z=1,from=1) 

失敗有語法錯誤「從」作爲一個關鍵字因。我怎麼能通過這樣的方式,沒有遇到語法錯誤?

回答

13

將其作爲字典傳遞。

func(**{'as': 'foo', 'from': 'bar'}) 
2
args = {'x':1, 'y':1, 'z':1, 'from':1} 
example.webrequest(**args) 

//不使用該庫

+1

這是Facebook的圖形API) – rutherford 2010-11-14 19:54:29

+0

或Twilio API還(SMS) – iutinvg 2012-11-01 02:23:09