max max - 3 months ago 6
Python Question

using python reserved keyword as variable name

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' ,
)


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

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

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

Answer

You can pass in arbitrary strings as keyword arguments using the **dictionary call syntax:

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.