Python Dict.get() Raises Keyerror
I am getting lost here, Python 2.7, I have a dictionary mt, and I use the get() method, which by documentation says: get(key[, default]) Return the value for key if key is in the
Solution 1:
So I nailed the problem down. Before this code was put in place there was this one
File "/home/ubuntu/subscription-workers/commands/dr/rebilling.py", line 48, in rebill
if mt['is_rebill'] == 1:KeyError:'is_rebill'
The problem was that there were .pyc files from the older version, but the stack trace was loading the actual code. After running
find . -name "*.pyc" -execrm -rf {} \;
and reloading the app everything was fine and without problems.
Solution 2:
>>>mt = {'key1' : 1}>>>mt.get('is_rebill', 0)
0
It does not generates key error if key is not present it returns 0
>>>mt.update({'is_rebill':1})>>>mt.get('is_rebill', 0)
1
>>>if mt.get('is_rebill', 0) == 1:...printTrue...else:...printFalse...
False
Post a Comment for "Python Dict.get() Raises Keyerror"