python: problem with dictionary get method default value

Posted by goutham on Stack Overflow See other posts from Stack Overflow or by goutham
Published on 2010-12-23T07:51:20Z Indexed on 2010/12/23 7:54 UTC
Read the original article Hit count: 217

Filed under:

I'm having a new problem here .. CODE 1:

try:
   urlParams += "%s=%s&"%(val['name'], data.get(val['name'], serverInfo_D.get(val['name'])))
except KeyError:
   print "expected parameter not provided - "+val["name"]+" is missing"
   exit(0)

CODE 2:

try:
   urlParams += "%s=%s&"%(val['name'], data.get(val['name'], serverInfo_D[val['name']]))
except KeyError:
   print "expected parameter not provided - "+val["name"]+" is missing"
   exit(0)

see the diffrence in serverInfo_D[val['name']] & serverInfo_D.get(val['name']) code 2 fails but code 1 works

the data

serverInfo_D:{'user': 'usr', 'pass': 'pass'} 
data: {'par1': 9995, 'extraparam1': 22}
val: {'par1','user','pass','extraparam1'}

exception are raised for for data dict .. and all code in for loop which iterates over val

© Stack Overflow or respective owner

Related posts about python