使用python保留关键字作为变量名 [英] using python reserved keyword as variable name

查看:638
本文介绍了使用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

推荐答案

您可以使用<$传递任意字符串作为关键字参数c $ c> ** 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.

这篇关于使用python保留关键字作为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆