Python Named Argument is Keyword?

◇◆丶佛笑我妖孽 提交于 2019-12-17 16:59:24

问题


So an optional parameter expected in the web POST request of an API I'm using is actually a reserved word in python too. So how do I name the param in my method call:

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

this fails with a syntax error due to 'from' being a keyword. How can I pass this in in such a way that no syntax error is encountered?


回答1:


Pass it as a dict.

func(**{'as': 'foo', 'from': 'bar'})



回答2:


args = {'x':1, 'y':1, 'z':1, 'from':1}
example.webrequest(**args)

// dont use that library



来源:https://stackoverflow.com/questions/4179175/python-named-argument-is-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!