为什么在twilioRESTAPI中“from”后面有下划线?

2024-10-01 17:35:05 发布

您现在位置:Python中文网/ 问答频道 /正文

twilio python library中,我们有以下功能来创建消息:

from twilio.rest import TwilioRestClient

我们可以写下:

msg = TwilioRestClient.messages.create(body=myMsgString, from_=myNumber, to=yourNumber)

我的问题很简单:为什么from参数后面有下划线?或者为什么这是参数名?是因为from在Python中是一个关键字,而我们用下划线后缀区分变量和关键字?在这种情况下真的有必要吗?在


Tags: fromimport功能rest消息参数createlibrary
1条回答
网友
1楼 · 发布于 2024-10-01 17:35:05

这是因为from将是一个无效的参数名,导致SyntaxError-这是一个python关键字。在

添加尾随下划线是避免PEP8 style guide中提到的冲突的建议方法:

If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption.

相关问题 更多 >

    热门问题