使用python保留关键字作为变量名

使用python保留关键字作为变量名

问题描述:

我正在尝试使用网络服务发送短信,这是网络服务文档建议的内容:

im trying to send sms using a webservice , this is what webservice document suggest :

response = client.service.SendSMS(  fromNum = '09999999' ,
                toNum = '0666666666666',
                messageContent = 'test',
                messageType = 'normal',
                user = 'myusername',
                pass = '123456' ,
           )

公平地说,他们没有python文档仅php / asp,所以我已经从他们的php示例中转换了它,但与我不同的是,有些人可能知道 pass 是python的保留关键字

to be fair they dont have document for python only php/asp so i've converted this from their php sample but as unlike me some may know pass is a reserved keyword of python

所以我不能有变量名 pass 因为我得到语法错误!

so i cant have variable name pass becuz i get syntax error !

有没有办法解决trhis或我应该切换到另一个Web服务的问题?我希望我们可以将变量名放在引号之内

is there a way around trhis or i should switch to another webservice ? i wish we could put variable names in quotation mark or something

您可以使用 ** dictionary 调用语法:

response = client.service.SendSMS(  fromNum = '09999999' ,
                toNum = '0666666666666',
                messageContent = 'test',
                messageType = 'normal',
                user = 'myusername',
                **{'pass': '123456'}
           )

您可以移动所有关键字如果需要,可以将参数放入字典中,并在应用之前将该字典分配给变量。

You can move all the keyword arguments into a dictionary if you want to, and assign that dictionary to a variable before applying.