Python 命名参数是关键字?

发布于 2024-10-02 05:35:11 字数 197 浏览 0 评论 0原文

因此,我正在使用的 API 的 Web POST 请求中预期的可选参数实际上也是 python 中的保留字。那么如何在方法调用中命名参数:

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

由于“from”是关键字,因此会失败并出现语法错误。我怎样才能以不会遇到语法错误的方式传递它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殤城〤 2024-10-09 05:35:11

将其作为字典传递。

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

Pass it as a dict.

func(**{'as': 'foo', 'from': 'bar'})
生生漫 2024-10-09 05:35:11
args = {'x':1, 'y':1, 'z':1, 'from':1}
example.webrequest(**args)

// 不要使用该库

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

// dont use that library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文