在twilio python library ,我们有这个功能来创建消息:
从 twilio.rest 导入 TwilioRestClient
我们可以这样写:
msg = TwilioRestClient.messages.create(body=myMsgString,from_=myNumber,to=yourNumber)
我的问题很简单:为什么下划线跟在 from
参数之后?或者为什么是参数名称?是不是因为 from
在 Python 中是关键字,我们用下划线后缀来区分变量和关键字?在这种情况下真的有必要这样做吗?
最佳答案
这是因为 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.
关于python - 为什么 Twilio Rest API 中的 "from"后面有一个下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708834/